Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a property to an existing type/control

Tags:

c#

wpf

subclass

Suppose I had a TextBox control, and I wanted to add a simple string property to it without having to create a new Textbox control which inherits from the regular TextBox control. Is that possible?

For example, something like:

TextBox tx = new TextBox();
// I want something like the following
// tx.addProperty("propertyname", "properyvalue", typeof(string));

Is there such thing in WPF/C#, and what is the simplest way to do this without having to create a new Textbox control which inherits from the regular TextBox control?

like image 917
user1590636 Avatar asked Jan 12 '23 13:01

user1590636


1 Answers

You can create a attached dependency property, and apply it to any type of control. For example, for TextBlock. Below is my example:

XAML

<Grid>
    <TextBlock Name="SampleTextBlock" Width="200" Height="30" 
               Background="AntiqueWhite" Text="Sample TextBlock" 
               local:MyDependencyClass.MyPropertyForTextBlock="TestString" />

    <StackPanel Width="100" Height="100" HorizontalAlignment="Left">
        <Button Name="GetValueButton" Content="GetValueButton" Click="GetValue_Click" />
        <Button Name="SetValueButton" Content="SetValueButton" Click="SetValue_Click" />
    </StackPanel>
</Grid>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void GetValue_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(MyDependencyClass.GetMyPropertyForTextBlock(SampleTextBlock));
    }

    private void SetValue_Click(object sender, RoutedEventArgs e)
    {
        MyDependencyClass.SetMyPropertyForTextBlock(SampleTextBlock, "New Value");

        MessageBox.Show(MyDependencyClass.GetMyPropertyForTextBlock(SampleTextBlock));
    }
}

public class MyDependencyClass : DependencyObject
{
    public static readonly DependencyProperty MyPropertyForTextBlockProperty;

    public static void SetMyPropertyForTextBlock(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(MyPropertyForTextBlockProperty, value);
    }

    public static string GetMyPropertyForTextBlock(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(MyPropertyForTextBlockProperty);
    }

    static MyDependencyClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata(string.Empty);

        MyPropertyForTextBlockProperty = DependencyProperty.RegisterAttached("MyPropertyForTextBlock",
                                                            typeof(string),
                                                            typeof(MyDependencyClass),
                                                            MyPropertyMetadata);
    }
}

Or you can use a property Tag, it just has been created to store additional information. But sometimes, this property can be occupied by other objectives, or may not hold because of his name. Far better to create their property with an intuitive name, for example: ValueForAnimation, StringId, CanScrolling, etc.

like image 86
Anatoliy Nikolaev Avatar answered Jan 18 '23 06:01

Anatoliy Nikolaev