When a template author in WPF tries to modify the look of
the control , it is the controltemplate of the control that get changed.
To retain core functionality of a control , the template
looks for some named elements with fixed names.If it is not found in the
template , the core functionality of the control won’t work.
Eg.
Lets say we are creating a textbox which has an image as
below.
<TextBox Width="150" Height="80">
<TextBox.Template>
<ControlTemplate>
<StackPanel>
<Image Source="tab_active.png"/>
</StackPanel>
</ControlTemplate>
</TextBox.Template>
</TextBox>
This code will get executed but you can’t type in anything
which is one of the core functionality of a textbox.It is because the template
of the textbox assumes a name “PART_ContentHost” to be present in the template
so that it can apply the core functionality of text box to it. The
“PART_ContentHost” named part for a textbox can be named to a ScrollViewer or
any control derived from AdornerDecorator (Decorator,Border etc…). Lets modify
out textbox with this named part,
<TextBox Width="150" Height="80">
<TextBox.Template>
<ControlTemplate>
<StackPanel>
<Image Source="tab_active.png"/>
<ScrollViewer Name="PART_ContentHost" />
</StackPanel>
</ControlTemplate>
</TextBox.Template>
</TextBox>
A way to find these named parts is digging into the definition
of the control or its base.The TemplatePart attribute contains the named parts
of the control.
E.g.
[TemplatePart(Name = "PART_ContentHost",
Type = typeof(FrameworkElement))]
[Localizability(LocalizationCategory.Text)]
public abstract class TextBoxBase
: Control
for combobox ....
[Localizability(LocalizationCategory.ComboBox)]
[TemplatePart(Name = "PART_EditableTextBox",
Type = typeof(TextBox))]
[TemplatePart(Name = "PART_Popup",
Type = typeof(Popup))]
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(ComboBoxItem))]
public class ComboBox : Selector
Hope this helps.
No comments:
Post a Comment