How to add a property to an existing type / control

Suppose I have a TextBox control and I want to add a simple string property to it without having to create a new Textbox control that inherits from a regular TextBox control. Is it possible?

For example, something like:

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

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

+4
source share
2 answers

You can create a related 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 the Tag property, it is simply created to store additional information. But sometimes this property may be occupied by other purposes or may not take place due to its name. It is much better to create your own property with an intuitive name, for example: ValueForAnimation , StringId , CanScrolling , etc.

+5
source

In general, the best way to do this is to inherit from the TextBox control. Unlike javascript, C # is a statically typed language , so you cannot just add such properties.

Since the Textbox control is a DependencyObject, you can also use attached properties - see above for this Anatoly Nikolaev. This may be better depending on how you want to use the property.

If you just want to add one piece of information, you can use the Tag property for the text field, which is designed for this purpose, and can accept any object. If you want to add more than one piece of information, you can set the Tag property to a value dictionary.

+4
source

Source: https://habr.com/ru/post/1498735/


All Articles