在ItemsControl中的每个项目周围包装一些东西 [英] Wrap something around each item in an ItemsControl

查看:69
本文介绍了在ItemsControl中的每个项目周围包装一些东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一系列不同类别的对象。每个类在资源文件中都有其UserControl DataTemplated。

Let's say I have a collection of objects of different classes. Each class has its UserControl DataTemplated in a resource file.

现在,我想使用ItemsControl来显示集合,但是我希望每个项目周围都有边框或扩展器。

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>

但是ContentPresenter似乎选择了ItemTemplate,因为我发生了堆栈溢出。

But the ContentPresenter seems to pick ItemTemplate, because I get a stack overflow.

如何在ItemTemplate中获取每个项目的DataTemplate?

How do I get each Item's DataTemplate inside the ItemTemplate?

推荐答案

通常您可以考虑这样做通过模板化项目容器。问题是通用 ItemsControl 使用 ContentPresenter 作为其项目容器。因此,即使尝试使用 ItemContainerStyle 设置样式,您也会发现您无法提供模板,因为 ContentPresenter 不会支持控制模板(它确实支持数据模板,但此处未使用)。

Normally you might consider doing this by templating the item container. The problem is the "generic" ItemsControl uses the ContentPresenter as its item container. So even if you try and set a style with ItemContainerStyle you will find you cannot supply a template because the ContentPresenter does not support control templating (it does support data templating but no use here).

要使用可模板化的容器,您必须从 ItemsControl出发code>像这样示例

To use a templatable container you will have to derrive from ItemsControl like in this example.

另一种选择可能只是使用 ListBox 控件。然后,您可以通过设置样式来设置 ListBoxItem 模板来提供自定义模板。

An alternative might be just to use the ListBox control instead. Then you can just provide a custom template by setting a ListBoxItem template via a style.

您可以阅读有关的更多信息容器此处

You can read more about containers here .

(请允许,我正在为您的答案添加解决方案,古格)

(With your permissen I'm adding the solution to your answer, Guge)

    <ListBox ItemsSource="{Binding MyObjects}" Grid.Column="1">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border BorderBrush="Black" BorderThickness="3">
                                <ContentPresenter/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

这篇关于在ItemsControl中的每个项目周围包装一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆