Dependency properties and attached properties

Source: Internet
Author: User

Dependency property: A property that can own no value and can derive a value from a data source by using a binding (dependent on someone else).

Let's take a small example: the value of the second text box is dependent on the first text box, which is what the first one loses, and the second shows what.

Let's write a simple XAML page:

<Windowx:class= "Mydependencyproperty.window1"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "Window1"Height= "Max"Width= "$">    <StackPanel>        <TextBoxx:name= "TextBox1"BorderBrush= "Black"Margin= "5"/>        <TextBoxx:name= "TextBox2"BorderBrush= "Black"Margin= "5"/>    </StackPanel></Window>
    • Then we need to write the dependency property, the dependency property is always used with these three modifiers: public static readonly.
    • It is declared and instantiated, and is produced using the Dependencyproperty.register method.
    • For ease of use, we will wrap the CLR wrapper on the dependency property. This way, when using dependency properties, it is the same as using a generic CLR property.
    • The SetBinding wrapper is the value of the textBox1 that the person class can use to bind the interface to the binding method.

Implement a class that has a dependency property:

 Public classPerson:dependencyobject {/// <summary>        ///CLR Property wrapper/// </summary>         Public stringName {Get{return(string) GetValue (NameProperty); }            Set{SetValue (nameproperty, value);} }        /// <summary>        ///dependency Property/// </summary>         Public Static ReadOnlyDependencyProperty NameProperty =Dependencyproperty.register ("Name",typeof(string),typeof(person)); /// <summary>        ///setbinding Packaging/// </summary>        /// <param name= "DP" ></param>        /// <param name= "binding" ></param>        /// <returns></returns>         Publicbindingexpressionbase setbinding (DependencyProperty DP, binding binding) {returnBindingoperations.setbinding ( This, DP, binding); }    }

We then use the binding to correlate the person's object per to the TextBox1, and the binding chain is formed by associating the textBox2 with the person object per.

The dependency property uses the following code:

 Public Window1 ()        {            InitializeComponent ();             New Person ();             New Binding ("Text") {Source = textBox1});             New Binding ("Name") {Source = per});        }

Operating effect:

Attached property: Refers to an attribute that does not belong to an object, but is appended to a specific requirement, that is, a property that is placed in a particular environment.

For example, people have a lot of attributes, when people in school, there will be grade this attribute, this attribute is not necessary always have, is in the school and give attached

Human class:

class Human:dependencyobject    {     }

One more school class:

classSchool:dependencyobject { Public Static intgetgrade (DependencyObject obj) {return(int) obj.        GetValue (Gradeproperty); }         Public Static voidSetgrade (DependencyObject obj,intvalue) {obj.        SetValue (gradeproperty, value); }        //Using a DependencyProperty as the backing store for Grade. This enables animation, styling, binding, etc ...         Public Static ReadOnlyDependencyProperty Gradeproperty =dependencyproperty.registerattached ("Grade",typeof(int),typeof(School),NewUIPropertyMetadata (0)); }

We can see that:

    • Gradeproperty is the DependencyProperty type member variable.
    • The public static readonly three modifiers are also used when declaring a declaration.
    • The difference is that the method used to register the attached property is: registerattached (), but the parameter is the same as the register.
    • There are different wrappers: the dependency property uses CLR properties to wrap the GetValue and SetValue two methods, and the attached property is wrapped with two self-named methods that are decorated with public static void.

Using attached properties, such as clicking on the Grade button on the interface is triggered:

Private void Btn_grade_click (object  sender, RoutedEventArgs e)        {            new  Human ( );             6 );             int grade = School.getgrade (human);            MessageBox.Show (grade. ToString ());        }

Its front-desk interface:

< Grid >        <  Content= "Grade"  Width= " Height=" x:name = "Btn_grade" Click = "Btn_grade_click" />    </ Grid >

Run as:

Dependency properties and attached properties

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.