learning SL / WPF, Dependency Properties (dependent attributes) is a new (strange) but the concept can not be avoided. -
http://www.wpftutorial.net/DependencyProperties.html -
There is a user-friendly
articles, but the text is E,
polo ralph lauren shirts, E Wen bad friends, you can refer to the following content: -
1. Why is there a dependency property? -
-
first recall the traditional attributes (Property): Usually we read or assignment of a property, the property is actually a private members behind the read and write. So more and more as the object's properties, together with objects derived from objects out of children, child objects regenerate \Each member must be allocated a private memory,
ralph lauren online, take some resources. -
-
But then think about it, we usually use a control / object, often only use a certain number of attributes, most of the property (sometimes even up to 90%) are using the default value (or you can understood as not used), which for WPF / SL is undoubtedly a great performance loss. -
-
so in this context, DP (Dependency Properties for short) arose,
ralph lauren clothing, we again recall that the static (static) method or the members, static member / method call does not depend on instances, it is the class level, No matter how many instances of this class, only static members in a memory, which is what we want! -
-
2. dependent on the general principles of property and benefits -
-
dependent properties of all objects with are inherited from DependencyObject,
ralph lauren womens dresses, DependencyObject, there is a \Han txt.Left = xxx can not be directly used to direct assignment, but must txt.SetValue (Canvas.Left, xxx) to deal with,
ralph lauren shop, because the static member is not an instance of the call. -
-
DP benefits: -
(1) to reduce memory consumption. -
(2) the direct successor to higher property values (here also explains why a higher level control automatically controls the layout of the lower levels, because the lower control automatically inherits the superior control of the relevant property values) -
(3) automatically a \
-
3. dependent on the reading strategy of property value -
-
-
This figure illustrates
GetValue and SetValue strategy of internal read. -
-
4. dependent on the use of sample properties: -
-
Here we demonstrate how to add custom control to a user Message dependency properties -
-
(1) First build a Silverlight User Control,
ralph lauren bikinis, named MyControl -
Java code -
-
-
CS components: (Tips: vs2008, simply type the propdp, then double tap the secondary Tab key, vs a dependency property will automatically add the code template) -
Java code -
using System.Windows; using System.Windows.Controls;
namespace DPStudy {public partial class MyControl: UserControl {public static readonly DependencyProperty MessageProperty = DependencyProperty.Register (\
public string Message { ; get {return (string) GetValue (MessageProperty);} ; set {SetValue (MessageProperty, value);}}
/ / /
/ / / Message handling notification of changes
/ / /
/ / /
/ / / Message handling notification of changes / / / / / /
/ / /
-
Here we define the string of a Message type dependent attributes, and the general properties of the difference is: you must use DependencyProperty.Register to register the property, and \SetValue / GetValue static methods to read its value, and finally we can also add a \-
-
(2) the trial MyControl into small chopper MainPage.xaml -
MainPage.Xaml as follows: -
Java code -
-
-
MainPage.Xaml.cs as follows: -
Java code -
using System.Windows; using System.Windows.Controls;
namespace DPStudy {public partial class MainPage: UserControl { public MainPage () { ; InitializeComponent (); ;}
private void Button_Click (object sender, RoutedEventArgs e) { ; myctl.SetValue (MyControl.MessageProperty, \;}}} using System.Windows; using System.Windows.Controls; namespace DPStudy { ; public partial class MainPage: UserControl {public MainPage () ; {InitializeComponent (); } ; private void Button_Click (object sender, RoutedEventArgs e) { myctl.SetValue (MyControl.MessageProperty, \
-
running, click the button to change the Message property value MyControl control, while MyControl the text in the text control will become a \