简体   繁体   English

不能同时使用 ItemTemplate 和 ItemContainerStyle 吗?

[英]Can't use ItemTemplate and ItemContainerStyle together?

I'm trying to apply both an ItemTemplate and ItemContainerStyle to an ItemsControl:-我正在尝试将 ItemTemplate 和 ItemContainerStyle 应用于 ItemsControl:-

<ItemsControl ItemsSource="{Binding LogEntries}"
              ItemTemplate="{StaticResource itemTemplate}"
              ItemContainerStyle="{StaticResource itemContainer}" />

However the ItemContainerStyle seems to be getting ignored (but it does work if I remove the ItemTemplate).然而 ItemContainerStyle 似乎被忽略了(但如果我删除 ItemTemplate 它确实有效)。

The item template is fairly complex and is used in a number of different views.项目模板相当复杂,并在许多不同的视图中使用。 In one specific view I need to change the spacing and background colour of the list items, hence why I was also trying to apply an ItemContainerStyle, which looks like this:-在一个特定的视图中,我需要更改列表项的间距和背景颜色,因此我还尝试应用 ItemContainerStyle,如下所示:-

<Style x:Key="itemContainer"
       TargetType="ContentPresenter">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border x:Name="itemBorder"
                        Margin="4,0,4,4"
                        Background="#666666">
                    <ContentPresenter Content="{Binding}" />
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

I'm a bit surprised that you can't apply both, unless I'm missing something?我有点惊讶你不能同时应用两者,除非我遗漏了什么? I assumed the ItemContainerStyle was really just a "wrapper" around the item content, regardless of whether or not the item's content is templated?我假设 ItemContainerStyle 实际上只是项目内容的“包装器”,无论项目内容是否已模板化?

The ItemContainerStyle is not a 'wrapper' for anything... it is a Style . ItemContainerStyle不是任何东西的“包装器”……它是Style You can set both the Style of the item container and the ItemTemplate property, but your problem is caused because you are trying to set the ContentTemplate property of the ContentPresenter in your Style and this is overwritten with the value of the ItemTemplate .可以同时设置项目容器的StyleItemTemplate属性,但您的问题是因为您试图在您的Style中设置ContentPresenterContentTemplate属性,并且它被ItemTemplate的值覆盖。 (See @Clemens' link in the comments section). (请参阅评论部分中的@Clemens 链接)。

One way to get around this is to use a ListBox that wraps its data items in ListBoxItem s and to provide a value for the Template property instead of the ContentTemplate .解决此问题的一种方法是使用将其数据项包装在ListBoxItem中的ListBox并为Template属性而不是ContentTemplate提供值。 (You can of course add a Style to remove it's borders to make it look like an ItemsControl ). (你当然可以添加一个Style来移除它的边框,让它看起来像一个ItemsControl )。 In this case, the ItemContainerStyle will affect the ListBoxItem instead.在这种情况下, ItemContainerStyle将影响ListBoxItem However, you must understand the difference.但是,您必须了解其中的区别。

The ItemContainerStyle will affect the ListBoxItem , whereas the ItemTemplate is used to define the data object inside. ItemContainerStyle将影响ListBoxItem ,而ItemTemplate用于定义内部的数据对象。 Therefore, it is appropriate to define a Border in the ItemContainerStyle and define how the data looks in the ItemTemplate .因此,在ItemContainerStyle中定义Border并在ItemTemplate中定义数据的外观是合适的。 Try this:试试这个:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Margin="4,0,4,4" Background="#666666">
                            <ContentPresenter Content="{Binding}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I ran into the same problem using code snippets from stackoverflow.我使用来自 stackoverflow 的代码片段遇到了同样的问题。 The problem can be fixed if you let visual studio generate the default ItemContainerStyle.如果让 visual studio 生成默认的 ItemContainerStyle,则可以解决该问题。 It is quite a long template so I just delete the parts I didn't need.这是一个相当长的模板,所以我只删除了不需要的部分。

In visual studio click on the ListView in designer, in the properties windows scroll down to the miscellaneous section, then click on the down-arrow next to ItemContainerStyle, then click on convert to a new resource.在 visual studio 中单击设计器中的 ListView,在属性窗口中向下滚动到杂项部分,然后单击 ItemContainerStyle 旁边的向下箭头,然后单击转换为新资源。

WARNING there are two very similar items on the list (ItemContainerStyle and ItemContainerStyleSelector) pick the right one.警告列表中有两个非常相似的项目(ItemContainerStyle 和 ItemContainerStyleSelector)选择正确的。 It is easy to miss if the properties window is not wide enough.如果属性窗口不够宽,很容易错过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ItemsControl、ItemContainerStyle 和 ItemTemplate - ItemsControl, ItemContainerStyle and ItemTemplate ListBox ItemContainerStyle和ItemTemplate交互 - ListBox ItemContainerStyle and ItemTemplate interaction WPF将ItemContainerStyle中的属性绑定到ItemTemplate的DataTemplate中的TextBlock - Wpf bind property in ItemContainerStyle to TextBlock in DataTemplate of ItemTemplate 奇怪的行为WPF TreeView ItemContainerStyle和ItemTemplate - Strange Behaviour WPF TreeView ItemContainerStyle and ItemTemplate 如何在ItemContainerStyle中使用EventToCommand? - how to use EventToCommand in a ItemContainerStyle? WPF ListBox中ItemTemplate和ItemContainerStyle有什么区别? - What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox? 如何让StackPanel使用ItemTemplate? - How can I get StackPanel to use an ItemTemplate? 如何在带有ItemContainerStyle的ListView中使用DisplayMemberPath? - How to use the DisplayMemberPath in a ListView with an ItemContainerStyle? WPF自定义MultiSelector将不使用ItemTemplateSelector(但将使用ItemTemplate) - WPF custom MultiSelector won't use ItemTemplateSelector (but will use ItemTemplate) 一种同时使用DisplayMemberPath和ItemTemplate的方法? - A way of using DisplayMemberPath and ItemTemplate together?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM