Original article connection http://virgos.javaeye.com/blog/592047
To learn SL/WPF, dependency properties (dependency attribute) is a completely new concept that cannot be avoided.
Http://www.wpftutorial.net/DependencyProperties.html
Here is an easy-to-understand articleArticleBut it's only e-text. If e-text is not good, you can also refer to the following content:
1. Why does the dependency attribute appear?
Think back to the traditional property: When we read or assign a value to an attribute, we actually read and write a private member behind the attribute. Then, as the object attributes become more and more, and the child objects derived from the object are added, the child object will generate a "Sun Tzu" object ...... in the final object running instance, there will be a large number of private members, and each private member must allocate memory, occupying certain resources.
However, when using a widget or object, we usually only use a few attributes, most of which (sometimes even more than 90%) all are default values (or can be understood as useless), which is undoubtedly a great performance loss for WPF/SL.
In this context, dp (short for dependency properties) appears. Let's look back at static (static) methods or members. The call of static members/methods does not depend on instances, it is class-level. No matter how many instances of this class, static members only occupy one portion in the memory. This is what we need!
2. General principles and benefits of dependency attributes
All objects with dependency attributes are inherited from dependencyobject. dependencyobject has a "Dictionary" storage area used to store dependency attributes, which are read in static mode, so now you should understand: Why can't you use TXT directly. left = xxx to directly assign values, but must use TXT. setvalue (canvas. left, XXX), because the static member cannot be called by the instance.
Advantages of DP:
(1) Effectively reduce memory consumption.
(2) directly inherit the property values of the upper-level controls (This also explains why the upper-level controls automatically layout the lower-level controls, because the lower-level controls automatically inherit the property values of the upper-level controls)
(3) "change notification" is automatically implemented (DP has a built-in change notification callback interface)
3. Read policy for dependency attribute values
This figure describes the internal read policies of getvalue and setvalue.
4. Example of dependency attributes:
The following example shows how to add the message dependency attribute to a custom control.
(1) first create a Silverlight user control named mycontrol.
Java Code
-
- <Usercontrol X: class ="Dpstudy. mycontrol"
- Xmlns =Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
- Xmlns: x =Http://schemas.microsoft.com/winfx/2006/xaml"
-
- >
-
- <Border cornerradius ="5"Borderthickness ="3"Borderbrush ="# Ffef410d"Width ="300"Height ="40"Margin ="5">
- <Textblock X: Name ="TXT"TEXT =""Verticalalignment ="Center"Horizontalalignment ="Center"> </Textblock>
-
- </Border>
-
- </Usercontrol>
<Usercontrol X: class = "dpstudy. mycontrol "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml"> <border cornerradius = "5" borderthickness = "3" borderbrush = "# ffef410d" width = "300" Height = "40" margin = "5"> <textblock X: name = "TXT" text = "" verticalignment = "center" horizontalalignment = "center"> </textblock> </Border> </usercontrol>
CS Section: (Tip: In vs2008, as long as you type propdp and press the secondary Tab key again, Vs will automatically add a code template with dependency attributes)
Java code
-
- Using system. windows;
-
- Using system. Windows. controls;
-
-
-
- Namespace dpstudy
- {
-
- PublicPartialClassMycontrol: usercontrol
-
- {
-
-
- Public StaticReadonly dependencyproperty messageproperty = dependencyproperty. Register ("Message", Typeof (string), typeof (mycontrol ),NewPropertymetadata ("Default value of message",NewPropertychangedcallback (onmessagepropertychanged )));
-
-
- PublicString message
-
- {
- Get {Return(String) getvalue (messageproperty );}
-
- Set {setvalue (messageproperty, value );}
-
- }
-
-
- /// <Summary>
-
- /// Notification processing when message changes
- /// </Summary>
-
- /// <Param name = "D"> </param>
-
- /// <Param name = "E"> </param>
-
- Private Static VoidOnmessagepropertychanged (dependencyobject D, dependencypropertychangedeventargs E)
-
- {
- Mycontrol CTL = D as mycontrol;
-
- Ctl.txt. Text = D. getvalue (mycontrol. messageproperty). tostring ();
-
- }
-
-
- PublicMycontrol ()
-
- {
-
- Initializecomponent ();
-
- This. Loaded + =NewRoutedeventhandler (mycontrol_loaded );
-
- }
-
-
- VoidMycontrol_loaded (Object sender, routedeventargs E)
-
- {
-
- This. Txt. Text = message;// The initial value of message is displayed during initial loading.
- }
-
- }
-
- }
Using system. windows; using system. windows. controls; namespace dpstudy {public partial class mycontrol: usercontrol {public static readonly dependencyproperty messageproperty = dependencyproperty. register ("message", typeof (string), typeof (mycontrol), new propertymetadata ("default value of message", new propertychangedcallback (onmessagepropertychanged ))); public String message {get {return (string) getvalue (messageproperty) ;}set {setvalue (messageproperty, value );}} /// <summary> /// handle the notification when the message changes /// </Summary> /// <Param name = "D"> </param> /// <Param name = "E"> </param> Private Static void onmessagepropertychanged (dependencyobject D, dependencypropertychangedeventargs e) {mycontrol CTL = D as mycontrol; ctl.txt. TEXT = D. getvalue (mycontrol. messageproperty ). tostring ();} public mycontrol () {initializecomponent (); this. loaded + = new routedeventhandler (mycontrol_loaded);} void mycontrol_loaded (Object sender, routedeventargs e) {this.txt. TEXT = message; // The initial value of message displayed during initial loading }}}
Here we define a message string-type dependency attribute, which is different from a common attribute: dependencyproperty must be used. register the property using register, and the "Name of the property" must be suffixed with "property". In addition, you must call the setvalue/getvalue static method to read the value during reading, finally, we can add a "Callback processing when the property value changes.
(2) Put mycontrol in mainpage. XAML.
The content of mainpage. XAML is as follows:
Java code
-
- <Usercontrol X: class ="Dpstudy. mainpage"
-
- 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: L ="CLR-namespace: dpstudy"
-
- MC: ignorable ="D"D: designwidth ="640"D: designheight ="480">
-
- <Stackpanel X: Name ="Layoutroot">
- <L: mycontrol X: Name ="Myctl"> </L: mycontrol>
-
- <Button click ="Button_click"Content ="Change message attribute value"Width ="130"> </Button>
-
- </Stackpanel>
-
- </Usercontrol>
<Usercontrol X: class = "dpstudy. mainpage "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: L =" CLR-namespace: dpstudy "MC: ignorable =" D "D: designwidth =" 640 "D: designheight =" 480 "> <stackpanel X: Name =" layoutroot "> <L: mycontrol X: name = "myctl"> </L: mycontrol> <button click = "button_click" content = "change message attribute value" width = "130"> </button> </stackpanel> </usercontrol>
The content of mainpage. XAML. CS is as follows:
Java code
-
- Using system. windows;
-
- Using system. Windows. controls;
-
- Namespace dpstudy
-
- {
-
- PublicPartialClassMainpage: usercontrol
-
- {
-
- PublicMainpage ()
-
- {
-
- Initializecomponent ();
-
- }
-
- Private VoidButton_click (Object sender, routedeventargs E)
-
- {
-
- Myctl. setvalue (mycontrol. messageproperty,"New Value");
-
- }
-
- }
-
- }
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, "New Value ");}}}
After running, click the button to change the message property value of the mycontrol control, and the text of the text control in mycontrol will also change to "new value"