我正在尝试为ListBoxItem控件设置一个ControlTemplate,现在我还没有做任何修改,它是开箱即用的:
<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>你知道为什么会发生这种情况吗?
谢谢。
发布于 2011-01-18 15:06:27
看起来你用的是ShowMeTheTemplate,但这并不总是能正常工作。
以下是来自ShowMeTheTemplate (Aero)的ListBoxItem模板的ContentPresenter:
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />和Blend中的相同部分:
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>还有其他不同之处,特别是这一行:
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"是什么原因导致您的DataTemplateSelector被绕过。我不能说我理解为什么,因为如果设置了TemplateSelector,ContentStringFormat应该被忽略。
教训是,我们需要一种更可靠的方法来提取与混合提取相同的模板/样式。
https://stackoverflow.com/questions/4719620
复制相似问题