You may give some template control names (magic names sometimes), but you might not be aware why is there the magic names, here is the deal.
Suppose that you were writing a text box, and suppose that you are the internal developer for the textbox, the requirement may sounds like this
You need to handle selection of text, including Commands like Ctrl+A
You need to handle Copy/Paste
You need to handle multiple languages and input methods
You need to position a caret between letters to show where the next keypress will be inserted
that is not enough, you are required to supports the following.
Users should be able to override everything about how the TextBox is displayed.
Below is one of the template that I wrote for the custom text box.
<TextBox>
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<Border>
<DockPanel>
<Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
<ScrollViewer />
</DockPanel>
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
Now, if you run it, you will find that you CANNOT input text, and there is no caret shown..
the reason is that there is no way to know where to render the caret, on the Image or on the ScrollViewer?
So , there is an assumption that the template will provide a control with specific name - PART_ContentHost
here is the revised code
<TextBox>
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<Border>
<DockPanel>
<Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
<ScrollViewer Name="PART_ContentHost/>
</DockPanel>
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
why this is important, let's see the code
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
anchor = Template.FindName("PART_ContentHost", this);
// Store the anchor, so we can position the caret over the top of it later
}
But, how do you know the magic string PART_ContentHost? it is defined by the templateParent attribute.
[TemplatePart(Name="PART_ContentHost", Type=typeof(FrameworkElement))]
public abstract class TextBoxBase : A500TypeDeepHierarchyOfClasses, IButNoInterfaces
And how can I search for the Parts and their names ? Let's take the text box for example. TextBox Styles and Templates
<TextBox>
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<Border>
<DockPanel>
<Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
<!-- ![CDATA[ without an anchor, the text box cannot show the caret;
An anchor point is a place where you know the text should go
<ScrollViewer />
]] -->
<!-- ![CDATA[
Follow up with the anchor point, you can do anything you like in the ControlTemplate, but you have to
remember to add a control with a specific name - in this case, PARTS_ContentHost
]] -->
<!--
<ScrollViewer Name="PART_ContentHost" />
-->
<ScrollViewer Name="PART_ContentHost" />
<!-- ![CDATA[The reason is because
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
anchor = Template.FindName("PART_ContentHost", this);
// Store the anchor, so we can position the caret over the top of it later
}
however, how do you know the magic strings?
It is applied with the TemplateParts attributes
e..g
[TemplatePart(Name="PART_ContentHost", Type=typeof(FrameworkElement))]
public abstract class TextBoxBase : A500TypeDeepHierarchyOfClasses, IButNoInterfaces
..
This example is based on the article by http://www.paulstovell.com/wpf-part-names
]]-->
</DockPanel>
</Border>
</ControlTemplate>
</TextBox.Template>
</TextBox>
The article is based on the original post of Pall Stovell, the origianl article is here