WPF 将两个集合合并到一个 DataGrid 中
我有两个集合,我想将它们一起显示在一张表中。我打算为此使用水平 DataGrid,并且我试图找出将集合公开给 DataGrid 的最佳方法。 (此处描述了水平 DataGrid 解决方案: WPF 水平 DataGrid )
有点无意义的例子,包括椅子和表。在我的应用程序中,我有一个桌子列表和一个椅子列表(桌子和椅子都有 Date 和 NumberOfItems 属性):
public List<Table> TableList { get; set; }
public List<Table> ChairList { get; set; }
在 xaml 中,我想将数据绑定到组合列表,以便我得到类似的内容:
Date | 01-Jan-2011 | 02-Jan-2011 | 03-Jan-2011 | 04-Jan-2011
Number of tables | 5 | 6 | | 7
Number of chairs | 3 | | 2 |
TableList 和 ChairList没有相同数量的物品。 公开可绑定组合集合的最佳方法是什么?
I have two collections that I want to display together in one table. I intend to use a horizontal DataGrid for this, and I'm trying to figure out what the best way is to expose the collections to the DataGrid.
(a horizontal DataGrid solution is described here: WPF horizontal DataGrid )
A bit of a nonsensical example with chairs and tables. In my application I have a list of tables and a list of chairs (both Table and a Chair have a Date and NumberOfItems property):
public List<Table> TableList { get; set; }
public List<Table> ChairList { get; set; }
In xaml I want to DataBind to a combined list so that I would get something like:
Date | 01-Jan-2011 | 02-Jan-2011 | 03-Jan-2011 | 04-Jan-2011
Number of tables | 5 | 6 | | 7
Number of chairs | 3 | | 2 |
The TableList and ChairList do not have the same number of items.
What would be the best approach to expose a bindable combined collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
CompositeCollection
类。 此处有一个很好的示例,包括展示如何为多种项目类型创建模板在综合列表中。这里的一个好处是,您可以在代码中创建集合并公开它以进行绑定,或者在 Xaml 中从两个单独绑定的集合构建它。Try the
CompositeCollection
class. There's a good example here, including showing how to template for multiple item types in the composite list. Once nice thing here is that you can either create the collection in code and expose it for binding, or build it in Xaml from two separately-bound collections.我最终将 DataGrid 绑定到 DataTable 的 DataView。我为每个日期创建了一个具有可为空的 Table 和 Chair 属性的 FurniturePerDate 对象,并使用 FurniturePerDate 的集合来创建 DataTable。非常不灵活,但它适用于我的用例。
I ended up binding the DataGrid to a DataTable's DataView. I created a FurniturePerDate object with nullable Table and Chair properties for each date, and used a collection of FurniturePerDate's to create the DataTable. Very inflexible but it works for my use case.