在 ItemsControl 中的每个项目周围包裹一些东西
假设我有不同类的对象的集合。每个类在资源文件中都有其 UserControl DataTemplated。
现在我想使用 ItemsControl 来显示集合,但我希望每个项目周围有一个边框或扩展器。
我希望这样的事情能够工作:
<ItemsControl ItemsSource="{Binding MyObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="3">
<ContentPresenter/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但是 ContentPresenter 似乎选择了 ItemTemplate,因为我遇到了堆栈溢出。
如何获取 ItemTemplate 中每个 Item 的 DataTemplate?
Let's say I have a collection of objects of different classes. Each class has its UserControl DataTemplated in a resource file.
Now I want to use ItemsControl to display the collection, but I want an Border or Expander around each item.
I would expect something like this to work:
<ItemsControl ItemsSource="{Binding MyObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="3">
<ContentPresenter/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But the ContentPresenter seems to pick ItemTemplate, because I get a stack overflow.
How do I get each Item's DataTemplate inside the ItemTemplate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您可能会考虑通过模板化项目容器来做到这一点。问题是“通用”
ItemsControl
使用ContentPresenter
作为其项目容器。因此,即使您尝试使用ItemContainerStyle
设置样式,您也会发现无法提供模板,因为ContentPresenter
不支持控件模板(它确实支持数据模板,但没有用)这里)。要使用模板化容器,您必须从
ItemsControl
派生,如下所示 示例。另一种选择可能只是使用
ListBox
控件。然后,您可以通过样式设置ListBoxItem
模板来提供自定义模板。您可以在此处阅读有关容器的更多信息 .
(经过您的许可,我将解决方案添加到您的答案中,古格)
Normally you might consider doing this by templating the item container. The problem is the "generic"
ItemsControl
uses theContentPresenter
as its item container. So even if you try and set a style withItemContainerStyle
you will find you cannot supply a template because theContentPresenter
does not support control templating (it does support data templating but no use here).To use a templatable container you will have to derrive from
ItemsControl
like in this example.An alternative might be just to use the
ListBox
control instead. Then you can just provide a custom template by setting aListBoxItem
template via a style.You can read more about containers here .
(With your permissen I'm adding the solution to your answer, Guge)
我只需执行以下操作:
由于
DataTemplate
标记内的数据上下文是源集合中的一个项目,因此我们可以使用ContentControl
来显示该项目。{Binding}
意味着我们要绑定到整个数据上下文。您的项目的所有DataTemplate
都将以与我们未指定ItemsControl.ItemTemplate
相同的方式隐式应用。I'd just do the following:
As data context inside
DataTemplate
tag is an item from the source collection, we can use aContentControl
to display this item.{Binding}
means that we're binding to the whole data context. AllDataTemplate
s for your items will be implicitly applied in the same way as if we didn't specifyItemsControl.ItemTemplate
.