Onur
Onur

Reputation: 5205

Binding and displaying a dictionary of dictionaries in WPF

I have a dictionary of dictionaries I want to display in a data grid.

var data = new Dictionary<KeyTypeA, Dictionary<KeyTypeB, string>>();

The "inner" dictionaries all share the same keys of a given set of KeyTypeB's which should become the row headers.

The question is related to this SO question, but the difference is that I don't know the keys until runtime.

Upvotes: 0

Views: 876

Answers (1)

KornMuffin
KornMuffin

Reputation: 2957

You can use the DataGrid and build a collection of DataGridColumn in code as follows (where ColumnCollection is an ObservableCollection<DataGridColumn>):

foreach ( string columnName in columns )
{
     ColumnCollection.Add( new DataGridTextColumn()
                           {
                               Header = columnName,
                               Binding = new Binding( String.Format( "[{0}]", columnName ) )
                           } );
}

You'll need to figure out how to get the columns collection based on you dictionaries and types.

You'll then need to bind the ColumnCollection to your DataGrid columns (see this SO answer for that).

Upvotes: 2

Related Questions