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.