ListBoxItem ControlTemplate и ItemTemplateSelector - PullRequest
1 голос
/ 18 января 2011

Я пытаюсь установить 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>

Это работает нормально, проблема в том, что я пытаюсь использовать ItemTemplateSelector в моем ListBox. Код 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 Ответ

3 голосов
/ 18 января 2011

Похоже, вы использовали ShowMeTheTemplate, и это не всегда работает корректно.

Вот ContentPresenter шаблон *1004* из ShowMeTheTemplate (Aero):

<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.экстракты.

...