Session 12 of WPF (Attached attribute)

Source: Internet
Author: User
Previously, I used three short posts to describe the Dependency attribute. Now, let's continue to look at a special Dependency attribute: Attached. The Attached attribute can be very efficient to Attach to other objects.

We still use the previous simple XAML code as an example:

<Window>

<StackPanel>

<Label> LabelText </Lable>

</StackPanel>

</Window>

How can I set the font size for StackPanel and its child elements? In the Window element, it has a FontSize attribute that can be directly set. However, StackPanel itself does not have such attributes as FontSize. This makes the Attached attribute appear. Here we need to use the Attached attribute FontSize defined in the TextElement element to set the font of the StackPanel.

<Window>

<StackPanel TextElement. FontSize = "30">

<Label> LabelText </Lable>

</StackPanel>

</Window>

In this way, the sub-element of StackPanel can get the new FontSize attribute through property value inheritance. For such a XAML Code, when the XAML compiler or parser sees this syntax, it requires that the TextElement (also known as the Attached property provider) have a static method SetFontSize to set the corresponding attribute value. Therefore, the above Attached attribute setting code can be implemented in C # as follows:

StackPanel panel = new StackPanel ();

TextElement. SetFontSize (panel, 30 );

The code here shows that the Attached attribute is not mysterious. Only call methods to associate elements with irrelevant properties. The SetFontSize implementation is also relatively simple. It only calls the DependencyObject. SetValue method called by the Dependency attribute access function. Note that the called object is the passed DependencyObject, rather than the current instance:

Public static void SetFontSize (DependencyObject element, double value)

{

Element. SetValue (TextElement. FontSizeProperty, value );

}

Similarly, the Attached attribute defines the corresponding GetXXX function. It calls the DependencyObject. GetValue method:

Public static double GetFontSize (DependencyObject element)

{

Return (double) element. GetValue (TextElement. FontSizeProperty );

}

Like the normal Dependency attribute, these GetXXX and SetXXX methods cannot do any additional work except for calling GetValue and SetValue.

In fact, in WPF applications, the Attached attribute is more used to control the UI layout. In addition to the StackPanel, Grid and so on.

Note: The above code has another issue to be explained. The TextElement element is used to set the font attribute of StackPanel. Why don't we use other elements like Control and Button?

The key to this problem is the Dependency attribute registration method. I used to describe the Dependency attribute [1. Let's take a look at the registration code of the FontSizeProperty attribute of Element:

TextElement. FontSizeProperty = DependencyProperty. RegisterAttached (

"FontSize", typeof (double), typeof (TextElement), new FrameworkPropertyMetadata (

SystemFonts. MessageFontSize, FrameworkPropertyMetadataOptions. Inherits |

FrameworkPropertyMetadataOptions. AffectsRender |

FrameworkPropertyMetadataOptions. AffectsMeasure ),

New ValidateValueCallback (TextElement. IsValidFontSize ));

This is similar to the previous IsDefault attribute, but the RisterAttached method optimizes the processing process of the attribute metadata required by the Attached attribute.

On the other hand, the FontSize attribute of Control calls the AddOwner Method on top of the attributes already registered by the TextElement element to obtain an identical instance reference:

Control. FontSizeProperty = TextElement. FontSizeProperty. AddOwner (

Typeof (Control), new FrameworkPropertyMetadata (SystemFonts. MessageFontSize,

FrameworkPropertyMetadataOptions. Inherits ));

Therefore, when implementing the Attached attribute, we use TextElement instead of Control.

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.