ListBoxItem ControlTemplate 和 ItemTemplateSelector

发布于 2024-10-12 05:32:01 字数 4259 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-10-19 05:32:01

您似乎使用了 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

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

It looks like you used ShowMeTheTemplate and this does not always work correctly.

Here is the ListBoxItem template's ContentPresenter from 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}" />

and the same section from Blend:

<ContentPresenter
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

and there are other differences as well but this line specifically:

    ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"

is what is the cause of your DataTemplateSelector being bypassed. I can't say that I understand why because ContentStringFormat is supposed to be ignored if TemplateSelector is set.

The moral is that we need a more reliable way to extract the same templates/styles that Blend extracts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文