Idle talk about wpf ii iii (ControlTemplate [1] in WPF)

Source: Internet
Author: User

Through the previous introduction, we know that WPF supports using Style Setters to modify the property value of a control to change its appearance. We know that any control of WPF has a visual tree and a logic tree. However, Style has its own limitations: it can only modify the attributes of the existing tree structure of the control, but cannot modify the tree hierarchy of the control. In practice, we often need to customize controls in a more advanced manner. In this case, you can use ControlTemplate.

In WPF, ControlTemplate is used to define the control's appearance. We can define a new ControlTemplate for the control to modify the structure and appearance of the control. Let's take an example first:

<Style TargetType = "Button">
<Setter Property = "OverridesDefaultStyle" Value = "True"/>
<Setter Property = "Template">
<Setter. Value>
<ControlTemplate TargetType = "Button">
<Grid>
<Ellipse Fill = "{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment = "Center"
VerticalAlignment = "Center"/>
</Grid>
</ControlTemplate>
</Setter. Value>
</Setter>
</Style>

The sample code shows that ControlTemplate contains the template semantics. That is to say, it affects multiple controls. This function can be implemented using Style. Therefore, after understanding the Style, such code should not be unfamiliar. First, set OverridesDefaultStyle to True, indicating that this control does not use any attributes of the current Themes. Use Setters to modify the Template attribute of the control. We define a new ControlTemplate to set the new value.

Similarly, ControlTemplate also uses the TargetType attribute, which has the same meaning as the TargetType of the Style. The same is true for its x: Key attribute. Then, a Grid is used to represent the visual content of the control. The TemplateBinding is similar to Binding, indicating that the display color of the current Ellipse is synchronized with the Background attribute of the Button. TemplateBinding can be considered as a special case of Binding in the template. Another ContentPresenter is related to the basic control type of WPF, ContentControl and ItemControl. In the preceding example, the Button is defined based on ContentControl. Therefore, ContentPresenter is used to display the content.

Each predefined control in WPF has a default template. Therefore, before learning a custom template (that is, a custom control), you can familiarize yourself with the default template of WPF. To view the tree structure hierarchy of a template, We can output the template as an XML file format, which is helpful for understanding.

XmlWriterSettings settings = new XmlWriterSettings ();
Settings. Indent = true;
Settings. IndentChars = new string ('', 4 );
Settings. NewLineOnAttributes = true;
StringBuilder strbuild = new StringBuilder ();
XmlWriter xmlwrite = XmlWriter. Create (strbuild, settings );
XamlWriter. Save (ctrl. Template, xmlwrite );

Ctrl is an instantiated Control class. And Control must be displayed on the screen; otherwise, Control. Template may be NULL.

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.