Having mocked up a custom control as a window and got all the behaviour right, I’m now trying to turn it into a proper custom control (called “When”, it’s a date-time widget).
I have prepared a When.XAML file in which sub-elements are named PART_xxx
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
xmlns:local="clr-namespace:Widgets">
<local:DatePartPositionValueConverter x:Key="DatePartPositionValueConverter" />
<local:DatePartVisibilityValueConverter x:Key="DatePartVisibilityValueConverter" />
<Style TargetType="{x:Type local:When}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:When}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Border BorderThickness="1" BorderBrush="{DynamicResource
{x:Static SystemColors.ControlDarkBrushKey}}">
<Grid HorizontalAlignment="Left" Margin="4,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
...
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock x:Name="PART_year" Grid.Column="{Binding
Converter={StaticResource DatePartPositionValueConverter},
ConverterParameter=y}">
<TextBlock.Text>
...
The custom control project Generic.XAML file references When.XAML
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Widgets;component/Themes/When.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
My code, however, does not seem to be able to resolve the PART_ names.
I’d like to be able to compare _focussedElement to (eg) PART_year to provide context for validation checks. It must be possible to refer directly to PART_xxx from the code of a custom control, otherwise it would be impossible to use code to bind event handlers to the elements of a template.
What have I failed to apprehend?
To paraphrase and expand on the excellent answer below, PART_year isn’t in scope because there’s no opportunity for the IDE’s code generation magic to bring it into scope. So you bring it into scope yourself, like this:
MenuItem PART_MenuItemToday, PART_MenuItemNow,
PART_MenuItemMonthEnd, PART_MenuItemMonthStart;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
PART_MenuItemMonthEnd = GetTemplateChild("PART_ContextMenuMonthEnd") as MenuItem;
PART_MenuItemMonthEnd.Click += PART_ContextMenuMonthEnd_Click;
...
}
When you need to hook the same set of handlers to several widgets you can do this
private void BindGenericHandlers(TextBlock textBlock)
{
textBlock.GotFocus += PART_GotFocus;
textBlock.LostFocus += PART_LostFocus;
textBlock.MouseDown += PART_MouseDown;
textBlock.MouseEnter += PART_MouseEnter;
textBlock.MouseLeave += PART_MouseLeave;
}
TextBlock _focussedElement, PART_year, PART_month, PART_day, PART_hour, PART_minute, PART_second, PART_designator;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
BindGenericHandlers(PART_day = GetTemplateChild("PART_day") as TextBlock);
BindGenericHandlers(PART_designator = GetTemplateChild("PART_designator") as TextBlock);
BindGenericHandlers(PART_hour = GetTemplateChild("PART_hour") as TextBlock);
BindGenericHandlers(PART_minute = GetTemplateChild("PART_minute") as TextBlock);
BindGenericHandlers(PART_month = GetTemplateChild("PART_month") as TextBlock);
BindGenericHandlers(PART_second = GetTemplateChild("PART_second") as TextBlock);
BindGenericHandlers(PART_year = GetTemplateChild("PART_year") as TextBlock);
...
}
You retreive references to your PARTs in the code by using the
GetTemplateChildmethod inside theOnApplyTemplateoverride. So in the code for your control, you’ll have the following:Since you are using PARTs, you seem to be making a lookless control, and therefore, you can’t have a direct reference to the element from the XAML (since a custom
ControlTemplatecould replace it with something unexpected). So you retrieve references to your PARTs with theGetTemplateChildmethod.NOTE: Be sure to use the lowest possible type for the part (in your code) in case someone replaces your expected control with a different implementation.