[英]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
.您可以同时设置项目容器的
Style
和ItemTemplate
属性,但您的问题是因为您试图在您的Style
中设置ContentPresenter
的ContentTemplate
属性,并且它被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.