具有GroupStyle的WPF ItemsControl ItemTemplate边框 [英] WPF ItemsControl ItemTemplate border with GroupStyle

查看:283
本文介绍了具有GroupStyle的WPF ItemsControl ItemTemplate边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发布图片,因此希望效果很好(一张图片值一千字,而我不想输入一千字).但是,下面的图像是我要完成的工作.

This is the first time I've posted a pic, so hopefully it turns out well (a picture is worth a thousand words, and I don't want to type a thousand words). But, the image below is what I'm trying to accomplish.

我有一个按属性"Group"分组的对象集合.我使用的是CollectionViewSource,它绑定到为我进行分组的数据源.

I have a collection of objects that I'm needing grouped by property "Group". I'm using a CollectionViewSource that is bound to my data source that is doing the grouping for me.

我正在使用ItemsControl控件(但可以轻松使用另一个控件)来显示此信息.我可以按属性将信息分组,但我希望能够用边框将整个组包围起来.我不想将整个组中的每个项目都包围起来.

I'm using an ItemsControl control (but could easily use another control) to display this information. I'm able to group the information by the property but I'd like to be able to surround the entire group with a border. I'm not wanting to surround each item in the group by the entire group.

如何完成下图所示的整个组的边框?

How do I accomplish something like the picture below with a border around the entire group?

推荐答案

类似的东西应该可以解决问题.使用它作为您的小组风格.您可能会想对它进行更多自定义,但是您应该能够从此代码片段中获得大致的想法.

something like this should do the trick. use this as your group style. You will probably want to customize this more, but you should be able to get the general idea from this snippet.

主要要知道的是您要绑定到GroupItem.基本上,GroupItem上有3个属性. (组的名称),ItemCount(分组中有多少个项目)和项目本身.

the main thing to know is that you are binding to a GroupItem. Basically there are 3 properties on a GroupItem. the Name (of the group), the ItemCount (how many items in your grouping) and the items themselves.

<ControlTemplate TargetType="{x:Type GroupItem}">

    <Border BorderBrush="Black" BorderThickness="1" Margin="5">
        <StackPanel>
            <TextBlock Text="{Binding Name}"/>
            <Border BorderBrush="Black" BorderThickness="1" Margin="0,0,0,0">
                <ItemsPresenter />
            </Border>
        </StackPanel>
    </Border>

</ControlTemplate>

对项目集合进行分组时,源不是项目的集合,而是GroupItems的集合,然后其中包含该集合中属于该组的项目.这就是为什么x:Type是GroupItem的原因.除了要显示的GroupItem的属性以外,这里不需要任何绑定.

When you Group a collection of items, the source is not a collection of your items, but a collection of GroupItems, wich then contains the items from your collection that belong to that group. this is why the x:Type is GroupItem. No binding is required here, other then to properties of the GroupItem that you wish to display.

您应该像这样将其放入您的<ItemControl> XAML中:

You should put this in your <ItemControl> XAML like so:

    <ItemsControl>
        <ItemsControl.GroupStyle>
<!-------------- style from above goes here --------------->
        <ItemsControl.GroupStyle/>
    <ItemsControl/>

此处是有关在WPF中进行分组以帮助您的文章.

here is an article on grouping in WPF to help you out.

这篇关于具有GroupStyle的WPF ItemsControl ItemTemplate边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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