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
- ItemsSource.Array2D & Array2DTransposed
- ItemsSource.RowsSource & ColumnsSource
- Selected.CellItem & Index
- ItemsSource.TransposedSource & PropertySource
- Rownumbers
For binding to sources of type T[,]
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.Array2D="{Binding Data2D}" />
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>
<DataGrid dataGrid2D:ItemsSource.Array2D="{Binding Data2D}"
dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}" />
<DataGrid dataGrid2D:ItemsSource.Array2DTransposed="{Binding Data2D}" />
Lets you bind to datasources of type IEnumerable<IEnumerable>>
.
Tracks collection changes.
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.RowsSource="{Binding ListOfListsOfInts}" />
<DataGrid HeadersVisibility="None"
dataGrid2D:ItemsSource.ColumnsSource="{Binding ListOfListsOfInts}" />
Limited support for different lengths. Columns with blanks are default readonly.
<DataGrid dataGrid2D:ItemsSource.RowsSource="{Binding DifferentLengths}" />
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}" />
Support for transposing an itemssource, perhaps useful for property grid scenarios. Supports binding to single item or (Observable)Collection
Same as TransposedSource but for a single item.
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>
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>