Gu.Wpf.DataGrid2D
Gu.Wpf.DataGrid2D copied to clipboard
Extension methods for WPF DataGrid enabling binding to T[,]
Gu.Wpf.DataGrid2D
Attached properties for WPF DataGrid enabling binding to sources of different types. For using DataGrid2D you need to add xmlns:dataGrid2D="http://gu.se/DataGrid2D" in XAML
Contents
- ItemsSource.Array2D & Array2DTransposed
- Array2D
- Explicit columns
- With headers:
- Array2DTransposed
- ItemsSource.RowsSource & ColumnsSource
- RowsSource
- ColumnsSource
- Different lengths
- Selected.CellItem & Index
- ItemsSource.TransposedSource & PropertySource
- PropertySource
- TransposedSource with explicit columns
- Rownumbers
ItemsSource.Array2D & Array2DTransposed
For binding to sources of type T[,]
Array2D
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.Array2D="{Binding Data2D}" />
Renders:

Explicit columns
Columns are referred to by C<zero_based_index>
<DataGrid AutoGenerateColumns="False"
dataGrid2D:ItemsSource.Array2DTransposed="{Binding Data2D}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding C0}" Header="Col 1" />
<DataGridTextColumn Binding="{Binding C1}" Header="Col 2" />
<DataGridTextColumn Binding="{Binding C2}" Header="Col 3" />
</DataGrid.Columns>
</DataGrid>
Renders:

With headers:
<DataGrid dataGrid2D:ItemsSource.Array2D="{Binding Data2D}"
dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}" />
Renders:

Array2DTransposed
<DataGrid dataGrid2D:ItemsSource.Array2DTransposed="{Binding Data2D}" />
Renders:

ItemsSource.RowsSource & ColumnsSource
Lets you bind to datasources of type IEnumerable<IEnumerable>>.
Tracks collection changes.
RowsSource
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.RowsSource="{Binding ListOfListsOfInts}" />
Renders:

ColumnsSource
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.ColumnsSource="{Binding ListOfListsOfInts}" />
Renders:

Different lengths
Limited support for different lengths. Columns with blanks are default readonly.
<DataGrid dataGrid2D:ItemsSource.RowsSource="{Binding DifferentLengths}" />
Renders:

Selected.CellItem & Index
Lets you two-way bind the item of the currently selected cell or index (row, col). For this to work these conditions must be satisfied:
SelectionUnit="Cell"- Columns must be of type
DataGridBoundColumn. Don't think there is a way to dig out the bound property of aDataGridTemplateColumn
<DataGrid SelectionUnit="Cell"
dataGrid2D:ItemsSource.RowsSource="{Binding RowVms}"
dataGrid2D:Selected.CellItem="{Binding SelectedItem}"
dataGrid2D:Selected.Index="{Binding Index}" />
ItemsSource.TransposedSource & PropertySource
Support for transposing an itemssource, perhaps useful for property grid scenarios. Supports binding to single item or (Observable)Collection
PropertySource
Same as TransposedSource but for a single item.
Renders:

TransposedSource with explicit columns
The property name column is named Name and the following columns are named C<zero_based_index>
<DataGrid AutoGenerateColumns="False"
dataGrid2D:ItemsSource.TransposedSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Property" />
<DataGridTextColumn Binding="{Binding C0}" Header="Value 1" />
<DataGridTextColumn Binding="{Binding C1}" Header="Value 2" />
</DataGrid.Columns>
</DataGrid>
Renders:

Rownumbers
Convenience attached property if you want to display rownumbers.
Specify the number to start fom using StartAt
<DataGrid ItemsSource="{Binding Persons}" dataGrid2D:Index.StartAt="1">
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Content" Value="{Binding Path=(dataGrid2D:Index.OfRow), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
</Style>
</DataGrid.RowHeaderStyle>
</DataGrid>
Renders:
