将自定义控件模拟为窗口并获得正确的所有行为后,我现在正尝试将其转换为适当的自定义控件(称为“时间”,它是一个日期时间小部件)。
我准备了一个 When.XAML 文件,其中子元素被命名为 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>
...
自定义控件项目 Generic.XAML 文件引用 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>
但是,我的代码似乎无法解析 PART_ 名称。
我希望能够将 _focussedElement 与(例如) PART_year 进行比较,以提供验证检查的上下文。必须可以从自定义控件的代码中直接引用 PART_xxx,否则无法使用代码将事件处理程序绑定到模板的元素。
我没有意识到什么?
为了解释和扩展下面的出色答案,PART_year不在范围内,因为 IDE 的代码生成魔法没有机会将其纳入范围。因此,您自己将其纳入范围,如下所示:
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;
...
}
当您需要将同一组处理程序挂钩到多个小部件时,您可以这样做
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);
...
}