ListBoxItem ControlTemplate 和 ItemTemplateSelector
我正在尝试将 ControlTemplate 设置为 ListBoxItem 控件,现在我还没有进行任何修改,它是开箱即用的:
<ControlTemplate TargetType="ListBoxItem"
x:Key="listBoxItemCustomTemplate">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
Name="Bd"
SnapsToDevicePixels="True">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected">
<Condition.Value>
<s:Boolean>True</s:Boolean>
</Condition.Value>
</Condition>
<Condition Property="Selector.IsSelectionActive">
<Condition.Value>
<s:Boolean>False</s:Boolean>
</Condition.Value>
</Condition>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这工作正常,问题是当我尝试在 ListBox 中使用 ItemTemplateSelector 时。 DataTemplateSelector 代码甚至无法运行,显然有一些东西阻止了 ItemTemplateSelector 在该 ControlTemplate 中工作,但我不确定如何运行。
这是 ListBox:
<ListBox x:Name="listBox"
ItemsSource="{Binding AllItems}"
ItemTemplateSelector="{DynamicResource ExperimentExplorerTemplateSelector}"
ItemContainerStyle="{DynamicResource customListBoxItemStyle}" />
以及设置 ControlTempalte 的样式:
<Style TargetType="{x:Type ListBoxItem}" x:Key="customListBoxItemStyle">
<Setter Property="Template" Value="{StaticResource listBoxItemCustomTemplate}" />
</Style>
有什么想法为什么会发生这种情况吗?
谢谢。
I'm trying to set a ControlTemplate to the ListBoxItem control, right now I haven't made any modification, it's as it comes out of the box:
<ControlTemplate TargetType="ListBoxItem"
x:Key="listBoxItemCustomTemplate">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
Name="Bd"
SnapsToDevicePixels="True">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected">
<Condition.Value>
<s:Boolean>True</s:Boolean>
</Condition.Value>
</Condition>
<Condition Property="Selector.IsSelectionActive">
<Condition.Value>
<s:Boolean>False</s:Boolean>
</Condition.Value>
</Condition>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
This works fine, the problem is when I try to use an ItemTemplateSelector in my ListBox. The DataTemplateSelector code doesn't even run, apparently there's something preventing the ItemTemplateSelector from working in that ControlTemplate, but I'm not sure how.
This is the ListBox:
<ListBox x:Name="listBox"
ItemsSource="{Binding AllItems}"
ItemTemplateSelector="{DynamicResource ExperimentExplorerTemplateSelector}"
ItemContainerStyle="{DynamicResource customListBoxItemStyle}" />
And the style that sets the ControlTempalte:
<Style TargetType="{x:Type ListBoxItem}" x:Key="customListBoxItemStyle">
<Setter Property="Template" Value="{StaticResource listBoxItemCustomTemplate}" />
</Style>
Any ideas why this is happening?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎使用了 ShowMeTheTemplate,但这并不总是能正常工作。
这是来自 ShowMeTheTemplate (Aero) 的
ListBoxItem
模板的ContentPresenter
:以及来自 Blend 的相同部分:
还有其他差异,但这一行具体是:
是什么原因您的
DataTemplateSelector
被绕过。我不能说我理解为什么,因为如果设置了TemplateSelector
,则应该忽略ContentStringFormat
。其寓意是,我们需要一种更可靠的方法来提取 Blend 提取的相同模板/样式。
It looks like you used ShowMeTheTemplate and this does not always work correctly.
Here is the
ListBoxItem
template'sContentPresenter
from ShowMeTheTemplate (Aero):and the same section from Blend:
and there are other differences as well but this line specifically:
is what is the cause of your
DataTemplateSelector
being bypassed. I can't say that I understand why becauseContentStringFormat
is supposed to be ignored ifTemplateSelector
is set.The moral is that we need a more reliable way to extract the same templates/styles that Blend extracts.