wpf--Dependency Properties (Dependency property)

Source: Internet
Author: User

1. What is a dependency property

A dependency property is a property that has no value on its own and obtains a value from the data source through a binding (dependent on someone else), and the object that owns the dependency property is referred to as the "dependent object."

The dependency property is registered in the WPF property system by calling the Register method (or registerreadonly), and the attribute is marked with the DependencyProperty identifier . Dependency properties can only be used by types that inherit from the DependencyObject class, and most of the classes in WPF support dependency properties.

2, DependencyObject and Dependencyporperty

DependencyObject and Dependencyporperty two classes are at the heart of the WPF property system

In WPF, the concept of dependent objects is implemented by the DependencyObject class, and the concept of dependency properties is implemented by the DependencyProperty class

A dependent object must be used as the host of the dependency property , and the two can be combined to achieve the complete binding target being driven by the data. The DependencyObject has a GetValue and SetValue two methods to get/set the value of the dependency property .

The declaration feature of the DependencyProperty instance-the reference variable is decorated by the public static readonly three modifier, which is not obtained by the new operator, but instead calls the Dependencyproperty.register method generation. (see example code)

3. Sample Code

<Windowx:class= "Dependencypropertydemo.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local= "Clr-namespace:dependencypropertydemo"mc:ignorable= "D"Title= "MainWindow"Height= " the"Width= "525">    <StackPanel>        <TextBoxx:name= "TextBox1"BorderBrush= "Black"Margin= "5"/>        <TextBoxx:name= "TextBox2"BorderBrush= "Black"Margin= "5"Text="{Binding path=text, elementname=textbox1,mode=oneway}"/>        <ButtonMargin= "5"Content= "OK"Click= "Button_Click"/>    </StackPanel></Window>

Declares a student class, inherits DependencyObject, declares a property in it, that is, the dependency property, and the naming convention is to end with the attribute.

For ease of use, give him a name attribute, call the SetValue () method in set Name,get call the GetValue () method to get the name.

   Public class Student:dependencyobject    {
    

<summary>
Description of each parameter in the Register method
The first argument is a string type, indicating that the wrapper with that CLR property as the dependency property, here is name, has implemented the wrapper for the CLR property
The second argument is type, which indicates what type of value the secondary dependency property is used to store
The third argument is type, which indicates the dependency property host type, or Dependencyproperty.register to which type the dependency property will be associated.
</summary>

 Public Static ReadOnlyDependencyProperty NameProperty = Dependencyproperty.register ("Name",typeof(string),typeof(Student));  Public stringName {Get{return(string) This. GetValue (Student.nameproperty); }            Set{ This. SetValue (student.nameproperty, value); }        }    }
Student Stu;  PublicMainWindow () {InitializeComponent (); Stu=NewStudent (); Binding binding=NewBinding ("Text") {Source =TextBox1};           Bindingoperations.setbinding (Stu, Student.nameproperty, binding); //textbox2.setbinding (textbox.textproperty, binding);        }        Private voidButton_Click (Objectsender, RoutedEventArgs e) {MessageBox.Show (stu).        Name); }

In the last two lines of code in MainWindow (), the binding student instance and Textbox,textbox as the data source, and the student instance as the target, the target property is NameProperty.

This implements the data binding of the foreground textbox input string and the student instance in the background.

wpf--Dependency Properties (Dependency 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.