在自定义控件样式中应用 DataTemplateSelector
我正在开发一个源自列表框的自定义控件。作为我的控件的一部分,我想提供一个默认的 ItemTemplateSelector。不幸的是,我无法让它找到我的 DataTemplateSelector。下面是代码,省略了不相关的细节:
public class AnnotationTemplateSelector : DataTemplateSelector
{
public DataTemplate BoxValuePairTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return BoxValuePairTemplate;
}
}
和 Xaml...
<DataTemplate x:Key="BoxValuePairDataTemplate">
<Grid TextBlock.FontSize="9" Height="13" Width="{Binding Path=Width}" SnapsToDevicePixels="False">
... etc...
</Grid>
</DataTemplate>
选择器:
<Annotations:AnnotationTemplateSelector BoxValuePairTemplate="{StaticResource BoxValuePairDataTemplate}"
x:Key="AnnotationTemplateSelector" />
样式:
<Style TargetType="{x:Type Annotations:BoxEditorSurface}">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="ItemTemplateSelector" Value="{StaticResource AnnotationTemplateSelector}"/>
... etc ...
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=TwoWay}" />
<Setter Property="Padding" Value="0" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
不幸的是,应用模板后,ItemTemplateSelector 始终为 null,并且没有显示绑定错误。
有想法吗?
I am developing a custom control which descends from a ListBox. As part of my control, I want to provide a default ItemTemplateSelector. Unfortunately, I can't see to get it to find my DataTemplateSelector. Here is the code, with irrelevant details elided:
public class AnnotationTemplateSelector : DataTemplateSelector
{
public DataTemplate BoxValuePairTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return BoxValuePairTemplate;
}
}
And the Xaml...
<DataTemplate x:Key="BoxValuePairDataTemplate">
<Grid TextBlock.FontSize="9" Height="13" Width="{Binding Path=Width}" SnapsToDevicePixels="False">
... etc...
</Grid>
</DataTemplate>
The selector:
<Annotations:AnnotationTemplateSelector BoxValuePairTemplate="{StaticResource BoxValuePairDataTemplate}"
x:Key="AnnotationTemplateSelector" />
The style:
<Style TargetType="{x:Type Annotations:BoxEditorSurface}">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="ItemTemplateSelector" Value="{StaticResource AnnotationTemplateSelector}"/>
... etc ...
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=TwoWay}" />
<Setter Property="Padding" Value="0" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
Unfortunately, after the template is applied, the ItemTemplateSelector is always null, and no binding errors are shown.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明我缺少 ContentPresenter 中的绑定。我现在提供我的答案,希望它能防止将来可能犯同样错误的人感到沮丧:
我缺少 ContentPresenter 中的 ContentTemplateSelector 绑定,因此它显然从未被调用。
It turns out I was missing a binding in the ContentPresenter. I'm providing my answer now in hopes it will prevent frustration for whomever might make the same mistake in future:
I was missing the ContentTemplateSelector Binding in the ContentPresenter, so it was obviously never invoked.