WPF Learning (4)-Additional attributes, wpf learning additional attributes
After cool for one night, I am just watching the martial arts novels of the story of dog blood: No spam martial arts, only spam martial arts ......
There is another saying: You are a user, not a customer, and you have the right to use it. Identify yourself!
No way. I have to learn from the good that the world says.
From the usage perspective, the additional attributes and dependency attributes are different.
The dependency attribute is defined to meet the binding technology requirements and implement data synchronization between objects.
Additional attributes are used to achieve the purpose that other objects have some of my attributes.
The first time I saw an application with an additional attribute, it appeared in The XAML document.
<Window>
<Grid>
<Button x: Name = "btn1" Grid. Row = "1"/>
</Grid>
</Window>
Button, which has no layout relationship. The outer Grid has some layout relationship attributes. As long as the Button is referenced, You can reposition it for yourself. What a magical implementation!
Additional attributes mean that the features I define can be modified by others. In other words, someone else described himself as an additional Property of mine.
Here, I define a name called "I"Host"Others" is calledSubscriberTo implement a simple use case with additional attributes.
Class HostObject: DependencyObject {public static string GetAttachedText (DependencyObject obj) {return (string) obj. getValue (AttachedTextProperty);} public static void SetAttachedText (DependencyObject obj, string value) {obj. setValue (AttachedTextProperty, value);} // I think this property should be called a visa. The registration process is to register public static readonly DependencyProperty AttachedTextProperty = DependencyProperty. registerAttached ("AttachedText", typeof (string), typeof (HostObject ));}Define the host class Order: DependencyObject {}Defines the subscription public static void TestAttachedProperty () {// Regular usage, from the host access value Order a = new Order (); HostObject. setAttachedText (a, "aaaa"); Console. writeLine (HostObject. getAttachedText (a); // then, the two objects access their own values Order B = new Order (); HostObject. setAttachedText (B, "bbbb"); Console. writeLine (HostObject. getAttachedText (B ));}Test Cases
We can see that there is no relationship between the class of the host and the consumer.
In terms of performance, the host provides the GetAttachedText and SetAttachedText methods, allowing the subscriber to subscribe to their own special registers, and finally it seems that the subscriber has some additional properties. Let's take a closer look at the implementation process of these two methods. In fact, the buyer calls the GetValue and SetValue methods to achieve the purpose of saving/value, rather than the host to do these things.
Through the previous day's learning, I already know that the place where the stored value of the additional property is neither in the host nor in the subscription, but is managed in a unified way using another container.
Objects with additional attributes must be derived from DependencyObject, so that the GetValue and SetValue methods are provided to access data from the container.
The host packs a sugar coat to itself to implement the GetAttachedText and SetAttachedText methods, which are represented by accessing the additional attributes of the consumer from the host object.
We can also access additional attributes from the subscriber.
Public static void TestAttachedProperty2 () {Order c = new Order (); c. setValue (HostObject. attachedTextProperty, "cccc"); string value = (string) c. getValue (HostObject. attachedTextProperty); Console. writeLine (value );}Access value from subscription
I can be more indulgent.
Public static void TestAttachedProperty3 () {DependencyProperty MyAttachedVisa = DependencyProperty. registerAttached ("MyAttachedRegisterName", typeof (string), typeof (SimpleClass); Order d = new Order (); d. setValue (MyAttachedVisa, "Hello MyAttachedRegister"); string value2 = (string) d. getValue (MyAttachedVisa); Console. writeLine (value2 );}Access data directly from additional attributes
It seems that as long as the class is derived from DependencyObject, its objects can be stretched to the container.
Let's look back at the previous magic Button and Grid. How can we achieve the layout? Use code to implement static snapshots that do not see the principle.
Public WindowTestAttachedProperty () {InitializeComponent (); Grid grid = new Grid (); grid. columnDefinitions. add (new ColumnDefinition (); grid. columnDefinitions. add (new ColumnDefinition (); grid. columnDefinitions. add (new ColumnDefinition (); Button button = new Button (); // you can use the static method of the host to set the Grid. setColumn (button, 1); // You can also use the user's instance method to set the button. setValue (Grid. columnProperty, 1); grid. children. add (button); this. content = grid ;}Code Implementation additional property call
The magical phenomenon is implemented here: grid. Children. Add (button );
Layout objects reach the shelf first, and define areas for child objects that may come in the future. When each sub-object is added, a position is assigned to the sub-object.
This logic is simple. After the additional attributes related to the layout are defined in the sub-object, when the layout object calls the AddChildren method,
These property values can also be obtained by layout objects. You can use these values to allocate locations.
Opening up a container so that both the host and the consumer can access the data in it is such a technique. Using WPF as the core technology, it shows many amazing phenomena.
In fact, it is necessary for programmers to give up the original power of structural design and call a lot of seemingly inappropriate sugar clothes. What programmers do is just what they look like.
If you worship it, you will never know how common it is. WPF is achieved with the most idiotic technique-flexibility and freedom!
Someone gave WPF a nickname-I admire it. For me, it took me a week and finally found that the technology I studied was so idiotic-I sprayed my meal!