First, the dependency property basic introduction
This article begins by learning about another important content dependency property of WPF.
As you all know, WPF brings a lot of new features, one of which is the introduction of a new property mechanism-dependency properties. Dependency properties appear to be used to implement features such as styles in WPF, automatic binding, and implementation animations. The appearance of dependency properties is derived from the special rendering principle of WPF, which differs from the. NET common attribute in that the value of a dependency property is determined by multiple providers and has built-in ability to pass change notifications.
Dependency properties are basically applied to all of the elements of WPF that need to be set properties. A dependency property determines its value based on multiple supplied objects (which can be animations, parent elements, bindings, styles, templates, and so on), and this value responds to changes in a timely manner.
A dependency property is a property that can get a value from a data source (depending on someone else) by its binding. Objects that have dependency properties are referred to as "dependent objects." Dependency properties focus on the word "dependent", since it is dependent, that is to say: the value of the dependency property change process must be related to other pairs, not a depends on B b depends on a, or interdependent.
Plainly, the so-called dependence, mainly applies in the following places:
1, two-way binding. With this, the dependency property does not write the amount of code, and do not have to implement what interface, it has its own two-way binding characteristics, for example, I put the name of the employee object to the Shake text box, once bound, as long as the value in the text box changes, dependency property employee names will also change, and vice versa;
2, Trigger. This thing is important in WPF, for example, a button background is red, I want it to hover over it as the background turns green, and once the mouse moves away, the button reverts to red.
If you're in traditional Windows programming, you'll have to find a way to get things done, or delegate, and write a bunch of code. Tell you, with dependency properties, you don't write a single line of code, and all processing is handled automatically by the WPF property system. The trigger only temporarily changes the value of the property, and when the touch is complete, the property value is automatically "restored".
3. Additional properties. An attached property is also a dependency property that can defer certain properties of type A to be set at run time based on the specifics of type B, and can simultaneously maintain the same property value at the same time by multiple types of objects, but the property values of each instance are independent.
4, a property changes, but also change the value of other properties, such as Toglebutton press the same time, pop-up drop-down box.
There are many new aspects of dependency properties compared to traditional CLR properties and object-oriented objects, including:
1, the introduction of new features: Added attribute change notification, restriction, verification and so on, so that we can make our application more convenient, but also make the code significantly reduced, many of the previously impossible features can be easily implemented.
2. Save memory: In project development such as WinForm, you will find that the properties of UI controls are usually given initial values, and storing a field for each attribute will be a huge waste of memory. WPF dependency properties allow an object to be created without the space used to store the data (that is, the space occupied by the field), leaving only the default value when data is needed. The ability to borrow data from other objects or to allocate space in real time----This object is called a dependent object and his ability to get data in real time relies on dependency properties. In WPF development, you must use dependent objects as a host of dependency properties to combine them to form a complete binding target that is driven by the data.
3, support multiple provide object: We can set the value of the dependency property in many ways. At the same time, its internal can store multiple values, with expression, Style, animation, etc. can bring us a strong development experience.
In. NET, for attributes, you should be familiar with the encapsulation class's fields, which represent the state of the class, compiled and converted to the corresponding get and set methods. Properties can be used by classes or structs, and so on. A simple property, as we commonly use it, is as follows:
12345678910111213141516171819202122232425262728293031 |
public
class
Student
{
string
m_Name;
public
string
Name
{
get
{
return
m_Name; }
set
{ m_Name = value; }
}
static Student()
{
}
}
|
Dependency properties are associated with normal. NET attribute is a common. NET attribute definition that only needs to define its set and get block assignments and settings. So how should dependency properties be defined? What is the difference and connection between dependency properties and properties? In fact, the implementation of the dependency property is also very simple, follow these steps to do it:
The first step: let your class inherit from the DependencyObject base class. In WPF, almost all UI elements inherit from DependencyObject, which encapsulates operations such as storing and accessing dependency properties, and using static types is related to the internal storage mechanism of dependency properties. WPF processing dependency properties no longer store property values in a private variable like normal. NET properties, but instead use a typical variable of a word to hold the value of the user display setting.
Step two: the definition of a dependency property must use public static to declare a DependencyProperty variable, and there is a property as the suffix, which is the true dependency attribute. For example, the following code defines a dependency property, NameProperty:
1 |
public static readonly DependencyProperty NameProperty; |
Step three: register the dependency property with the property system in the static constructor and get the object reference. The dependency property is created by calling the Dependencyproperty.register static method, which needs to pass a property name, which is very important when defining the control style and template. The value that is populated with the property attribute of the setter is the name used when registering the dependency property. PropertyType indicates the actual type of the dependency property, OwnerType indicates which class registered the dependency property, and finally typemetadata the meta information for the dependency property, including the default value used by the dependency property, and the notification function when the value of the property changed. For example, the following code registers a dependency property.
1 |
NameProperty = DependencyProperty.Register( "Name" , typeof ( string ), typeof (Student), new PropertyMetadata( "名称" , OnValueChanged)); |
Fourth Step: in the previous three steps, we completed a dependency property registration, then how can we read and write this dependency property? The answer is to provide a dependency property of the instantiation wrapper property, which allows for specific read and write operations. Unlike CLR properties, a dependency property is not manipulated directly on a private variable, but instead operates on a property value by using the GetValue () and SetValue () methods, which can be used in standard. NET attribute definition syntax to be encapsulated so that dependency properties can be used as standard properties, as in the code below.
123456789 |
public string Name { get { return ( string )GetValue(NameProperty); } set { SetValue(NameProperty, value); } } |
Based on the previous four steps, we can write the following code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
public
class
Student : DependencyObject
{
//声明一个静态只读的DependencyProperty字段
public static
readonly
DependencyProperty NameProperty;
static
Student()
{
//注册我们定义的依赖属性Name
NameProperty = DependencyProperty.Register(
"Name"
,
typeof
(
string
),
typeof
(Student),
new
PropertyMetadata(
"名称"
, OnValueChanged));
}
private
static
void
OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
//当值改变时,我们可以在此做一些逻辑处理
}
//属性包装器,通过它来读取和设置我们刚才注册的依赖属性
public
string
Name
{
get
{
return
(
string
)GetValue(NameProperty); }
set
{ SetValue(NameProperty, value); }
}
}
|
Summary: we generally. NET attribute is to encapsulate a private property of a class directly, so read the value, that is, read the field directly, whereas the dependency property is operated by calling GetValue () and SetValue that inherit from DependencyObject. It is actually stored in a IDictionary key-value pairing dictionary of DependencyProperty, so the key (key) in a record is the hashcode value of the property, and the value It is the DependencyProperty of our registration.
WPF Getting Started Tutorial series 11-Dependency properties (i)