3

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?

Community
  • 1
  • 1
eriksmith200
  • 2,159
  • 5
  • 22
  • 33

2 Answers2

9

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.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • In a horizontal DataGrid, using a CompositeCollection would result in 1 row with number of tables and number of chairs mixed, and 1 column per date in each source collection, so with the furniture example a duplicate column for 01-Jan-2011 – eriksmith200 Jan 24 '11 at 12:00
1

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.

eriksmith200
  • 2,159
  • 5
  • 22
  • 33