Talk about the dependency properties of WPF

Source: Internet
Author: User

first, let's take a look around the dependency property

What is a dependency property: The dependency property has no value for itself, and the value is obtained by relying on someone else (such as a binding).

Why the dependency property appears: the control characters commonly used segment is limited, wrapping too many properties can consume too much memory and cause waste. So with dependency properties, there's no need to use it.

How to declare a dependency property: decorated with public static readonly three modifiers.

How to declare an instance: generated using the Dependencyproperty.register method. This method has three parameters followed by four parameters.

How to manipulate the value of a dependency property: implemented using the GetValue, SetValue method of the dependent object (Dependency.

now, let's implement the dependency property in code:

Xaml

<window x:class= "Dependecyproperty.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:dependecyproperty "        mc:ignorable=" D "        Title=" MainWindow "height=" width= "525" > <StackPanel> <textbox/> <textbox x:name=        " TESTTB "/>    </StackPanel></Window>

Declaring a dependency property with a reference

  public class Student:dependencyobject    {        //<summary>///Declaration Dependency Properties///</summary>// Using a DependencyProperty as the backing store for NameProperty.  This enables animation, styling, binding, etc        ... public static readonly DependencyProperty namepropertyproperty =            dependencyproperty.register ("NameProperty", typeof (String), typeof (Student));    }
Reference

Public MainWindow ()
{
InitializeComponent ();
Instantiating classes
Student stu = new Student ();
Assigning a value to a dependency property
Stu. SetValue (Student.namepropertyproperty, "DFSDFSD");
Get the value of a dependency property
Testtb.text = stu. GetValue (Student.namepropertyproperty). ToString ();
}

The run program succeeds in getting the value of the dependency property. But you can't get the value of a dependency property directly in XAML? We can bind the dependency property to the text box

<window x:class= "Dependecyproperty.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:dependecyproperty "        mc:ignorable=" D "        Title=" MainWindow "height=" width= "525" >    <StackPanel>        <textbox/>        <!--When dependency properties change, text box characters are also notified, real-time change--        <textbox x:name= "TESTTB" text= "{Binding nameproperty,mode=twoway}"/>    </ Stackpanel></window>

Binding data at the post code

Instantiate class            Student stu = new Student ();            This. DataContext = Stu;

The dependency property is successfully obtained

Might have been asked here, declaring dependency properties It's troublesome to hit so many words, is there a convenient way? By hitting PROPDP and then pressing two times tab, Visual Studio automatically generates dependency properties, automatically generated dependency properties of Dependencyproperty.register

(Perhaps the observant person asks, does the new dependency property produce so many errors?) Because there are multiple error messages on the way. In fact, these errors are caused by declaring a dependency property in the namespace, which is a false demonstration, ignoring it.

What scenarios are dependency properties applied to?

Dependency properties can be used in scenes where the scene is changing, such as the text of a button changing under different circumstances. In this case we can inherit the interface INotifyPropertyChanged and then declare the change notification for the property. However, the performance cost of the relative dependency property is high, so the dependency property can be used in this case. It is also possible to borrow when other classes need to use this dependency property. The scenario for a general dependency property is a custom control.

  

Talk about the dependency properties of WPF

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.