首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListBoxItem ControlTemplate和ItemTemplateSelector

ListBoxItem ControlTemplate和ItemTemplateSelector
EN

Stack Overflow用户
提问于 2011-01-18 09:16:17
回答 1查看 4.1K关注 0票数 1

我正在尝试为ListBoxItem控件设置一个ControlTemplate,现在我还没有做任何修改,它是开箱即用的:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<ListBox x:Name="listBox"
         ItemsSource="{Binding AllItems}"
         ItemTemplateSelector="{DynamicResource ExperimentExplorerTemplateSelector}"
         ItemContainerStyle="{DynamicResource customListBoxItemStyle}" />

和设置ControlTempalte的样式:

代码语言:javascript
复制
<Style TargetType="{x:Type ListBoxItem}" x:Key="customListBoxItemStyle">
    <Setter Property="Template" Value="{StaticResource listBoxItemCustomTemplate}" />
</Style>

你知道为什么会发生这种情况吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-18 15:06:27

看起来你用的是ShowMeTheTemplate,但这并不总是能正常工作。

以下是来自ShowMeTheTemplate (Aero)的ListBoxItem模板的ContentPresenter

代码语言:javascript
复制
<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中的相同部分:

代码语言:javascript
复制
<ContentPresenter
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

还有其他不同之处,特别是这一行:

代码语言:javascript
复制
    ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"

是什么原因导致您的DataTemplateSelector被绕过。我不能说我理解为什么,因为如果设置了TemplateSelectorContentStringFormat应该被忽略。

教训是,我们需要一种更可靠的方法来提取与混合提取相同的模板/样式。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4719620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档