3. Dependency properties
When you are programming with WPF, you often encounter the term "dependency property." WPF elements are classes with methods, properties, and events. What does it mean that almost every property of a WPF element is a dependency property? Dependency properties can depend on other inputs, such as themes and user preferences. Dependency properties are used with data binding, animations, resources, and styles
As we said earlier in WPF architecture, only classes that derive from the DependencyObject base class can contain dependency properties.
(Since the DependencyObject class is at the highest level of the hierarchy, each WPF element derives from this base class)
Write a class-custom dependency property below
classCustomclass:dependencyobject {//encapsulation of CLR properties Public stringName {Get{return(string) GetValue (NameProperty); } Set{SetValue (nameproperty, value);} } //Defining Dependency Properties Public Static ReadOnlyDependencyProperty NameProperty = Dependencyproperty.register ("Name",typeof(string),typeof(Customclass)); }
The above class defines a dependency property of NameProperty and a Name property
Dependency properties Use the Register () method to register through the WPF dependency property system. The Register () method can get the name of the dependency property, the type of the dependency property, and the type of the owner. Using the SetValue () method of the DependencyObject base class, you can set the value of a dependency property and use the GetValue () method to get its value.
New Customclass (); Cs. SetValue (Customclass.nameproperty, txtbox1.text); = (string) cs. GetValue (Customclass.nameproperty);
Instantiate a custom class, set and get the value of a dependency property
The properties of WPF controls have dependency properties, so the properties are set
Here is the dependency property that sets a button
//set the background color of a button through the properties of the wrapperButtona.background =NewSolidColorBrush (colors.red);//setting the background color of a button through the SetValue of a dependency propertySolidColorBrush brush =NewSolidColorBrush (Colos.blue); Buttonb.setvalue (Button.backgroundproperty,brush);//get the background color of buttonb by wrapping propertiesSolidColorBrush BRUSH1 =(SolidColorBrush) (buttona.background); Txt_value1.text=Brush1. Color.tostring ();//get the background color of buttonb through the Getvaue of the dependency propertySolidColorBrush BRUSH2 =( SolidColorBrush) (Buttonb.getvalue (Button.backgroundproperty)); Txt_value2.text= B_Brush2.Color.ToString ();
The following scenarios for applying dependency properties are given in MSDN:
1. You want to set properties in the style.
2. You want the property to support data binding.
3. You want to use dynamic resource references to set properties.
4. You want to automatically inherit property values from the parent element in the element tree.
5. You want the properties to be animated.
6. You want the property system to report when the property system, environment, or user performs an action or reads and uses a style to change the previous value of the property.
7. You want to use established, WPF processes also use metadata conventions, such as reporting changes to property values when the stone Buddha requires the layout system to rewrite elements of the visual object.
Here are a few practical scenarios to apply
XAML (2)-dependency property