WPF learning dependencies and attached properties

Source: Internet
Author: User
Tags bool implement

Dependency properties (Dependency property)

The. NET Framework 3.0 introduces a new attribute type called a dependency property, which is used by WPF,WF to implement styling, data binding, and so on. We use dependency properties more so that the property values of the parent element are passed slowly into its child elements in the logical tree. This allows you to share property values in the entire logical child element of the parent element. WF relies on dependency properties to pass property values between activities within a workflow. Therefore, the ability to transfer change notifications built into a dependency property is the most important feature.

If you want the attribute to be valid and share values in an entire logical control tree that contains the content child control tree, you can simply declare the attribute as a dependency property, and WPF will support the sharing of the property through the built-in schema. In a workflow, we often need to use dependency properties, which ensure that multiple components share the same value in a workflow instance. Fortunately, most of the space in WPF is dependent on attributes, which makes it easy to apply, and you don't need to be behind what happens.

Implementation of dependency properties

Dependency properties are also common. NET property, simply registers the normal. Net property as a dependency property through the Dependencyproperty.register method. In the Declaration of dependent attributes, the corresponding ordinary. NET properties are not required because their internal getvalue and SetValue methods are exposed, and users of dependent properties can discard dependency on normal. NET properties by invoking Getvalue/setvalue. But built in the ordinary. NET familiarity is more in line with our usual practice, and this facilitates setting properties in XAML.

The following code shows the common practice of defining dependent properties:

public class Button:buttonbase
{
//dependent properties
public static readonly DependencyProperty Isdependencypro Perty;

Static Button ()
{
//Registration dependency property
Button.isdependencyproperty = Dependencyproperty.register ("is Default,
typeof (BOOL), typeof (Button),
New Frameworkpropertymetadata (False,
new Propertycha Ngedcallback (onisdefaultchanged)));
}
//. NET attribute (optional)
public bool IsDefault
{
Get {return (bool) GetValue (Button.isdefaultproperty);}
Set {SetValue (Button.isdefaultproperty, value);}
}

//Callback for property change
private static void Onisdefaultchanged (DependencyObject O,
Dependencypropertychange Deventargs e)
{...}
}

In the example code above, the standard way to implement dependency property definitions is to note that dependency properties are always defined in general. NET attribute, even though it is registered with the Register static method. NET attributes, such as the IsDefault property, are not required. Another area to note is that by declaring dependency properties, we have a callback method that controls the change in property (even if it can be defined by declaring delegates and events, but here we do nothing, why not?) The advantage is that we can do what we want to do when the attribute changes, but we omit the task of manually declaring the event. In fact, the callback function exists to make sure that in the property wrapper (the IsDefault property) only uses the standard getvalue/setvalue instead of any other logic, and instead writes the custom logic to the callback function--to follow the unified design principles of WPF, Keep the XAML settings properties consistent with the procedure Code settings property.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.