ListBoxItem ControlTemplate和ItemTemplateSelector

时间:2011-01-18 01:16:17

标签: wpf wpf-controls

我正在尝试将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>

为什么会发生这种情况?

感谢。

1 个答案:

答案 0 :(得分:3)

看起来您使用的是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被绕过的原因是什么?我不能说我理解为什么因为ContentStringFormat如果设置了TemplateSelector则应该被忽略。

道德是我们需要一种更可靠的方法来提取Blend提取的相同模板/样式。