WPF ItemsControl ItemTemplate 边框与 GroupStyle
这是我第一次发图片,所以希望效果很好(一张图片抵一千字,我不想打一千字)。但是,下图是我想要实现的目标。
我有一个需要按属性“组”分组的对象集合。我正在使用绑定到为我进行分组的数据源的 CollectionViewSource。
我正在使用 ItemsControl 控件(但可以轻松使用另一个控件)来显示此信息。我可以按属性对信息进行分组,但我希望能够用边框包围整个组。我不想让整个组包围组中的每个项目。
如何完成如下图所示的整个组周围有边框的操作?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情应该可以解决问题。将此作为您的团体风格。您可能想要更多地自定义它,但是您应该能够从此代码片段中获得总体思路。
主要要知道的是您正在绑定到 GroupItem。基本上,GroupItem 有 3 个属性。 (组的)名称、ItemCount(分组中有多少个项目)以及项目本身。
编辑:
当您对项目集合进行分组时,源不是项目的集合,而是 GroupItems 的集合,其中包含集合中属于该组的项目。这就是为什么
x:Type
是GroupItem。除了您希望显示的 GroupItem 的属性之外,此处不需要任何绑定。您应该将其放入
XAML 中,如下所示:这里有一篇关于 WPF 中分组的文章可以帮助您。
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.
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.
EDIT:
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.You should put this in your
<ItemControl>
XAML like so:here is an article on grouping in WPF to help you out.