WPF combine two collections in one DataGrid
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?
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.
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.
链接地址: http://www.djcxy.com/p/64726.html