DataGrid
Multi-column data grid with typed columns, sorting indicators, and row selection.
Overview
DataGrid extends Selector and provides a tabular data view
with typed columns, sortable headers, and row selection. Columns follow a WPF-aligned
type hierarchy:
DataGridColumn— abstract base with Header, Width, sorting configDataGridBoundColumn— abstract, addsBindingproperty (AOT-safe via PropertyAccessorRegistry)DataGridTextColumn— displays bound values as textDataGridCheckBoxColumn— displays boolean values as[x]/[ ]DataGridTemplateColumn— renders cells via aCellTemplateDataTemplate
When a column is sorted, the header displays ▲ for ascending and
▼ for descending. Column separators use │, header
lines use ─, and crossings use ┼.
Basic Example
<DataGrid ItemsSource="{Binding Employees}" SelectedIndex="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="Name" />
<DataGridTextColumn Header="Department" Binding="Department" />
<DataGridCheckBoxColumn Header="Active" Binding="Active" Width="10" />
</DataGrid.Columns>
</DataGrid>
Properties
| Property | Type | Default | Description |
|---|---|---|---|
| SelectedBackground | Color | Blue | Background color of the selected row. |
| SelectedForeground | Color | White | Foreground color of the selected row. |
| HeaderBackground | Color | DarkGray | Background color of the column header row. |
| HeaderForeground | Color | White | Foreground color of the column header row. |
| ShowGridLines | bool | true | Whether to display column separators and header line using box-drawing characters. |
| CanSort | bool | true | Whether sorting is enabled globally for the grid. |
| SelectedIndex | int | -1 | Zero-based index of the currently selected row. Inherited from Selector. |
| SelectedItem | object | null | The currently selected data item. Inherited from Selector. |
| ItemsSource | IEnumerable | null | Data source for generating rows. Inherited from ItemsControl. |
Column Types
DataGridColumn (abstract base)
| Property | Type | Default | Description |
|---|---|---|---|
| Header | string | "" | Text displayed in the column header. |
| Width | int | 0 (star) | Character width. 0 = fill remaining space (star sizing). |
| SortDirection | SortDirection | None | Current sort direction: None, Ascending, or Descending. |
| CanUserSort | bool | true | Whether this column can be sorted. |
| SortMemberPath | string? | null | Property path for sorting. Bound columns default to their Binding path. |
DataGridBoundColumn (abstract, extends DataGridColumn)
| Property | Type | Default | Description |
|---|---|---|---|
| Binding | string? | null | Property path on each data item. Resolved via PropertyAccessorRegistry (AOT-safe). |
DataGridTextColumn (extends DataGridBoundColumn)
Displays the bound value as text. Falls back to ToString() if Binding is not set.
DataGridCheckBoxColumn (extends DataGridBoundColumn)
Displays a boolean value as [x] (true) or [ ] (false), centered in the cell.
DataGridTemplateColumn (extends DataGridColumn)
| Property | Type | Default | Description |
|---|---|---|---|
| CellTemplate | DataTemplate? | null | Template used to render each cell. DataContext is set to the row's data item. |
Examples
Text and CheckBox Columns
<DataGrid ItemsSource="{Binding Employees}" SelectedIndex="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="Id" Width="6" />
<DataGridTextColumn Header="Name" Binding="Name" />
<DataGridTextColumn Header="Department" Binding="Department" />
<DataGridCheckBoxColumn Header="Active" Binding="Active" Width="10" />
</DataGrid.Columns>
</DataGrid>
Template Column
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="Name" />
<DataGridTemplateColumn Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}"
Foreground="Green" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Sorting Columns
<DataGrid CanSort="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="Name"
SortDirection="Ascending" />
<DataGridTextColumn Header="Score" Binding="Score"
CanUserSort="True" />
<DataGridCheckBoxColumn Header="Passed" Binding="Passed"
CanUserSort="False" />
</DataGrid.Columns>
</DataGrid>
Without Grid Lines
<DataGrid ShowGridLines="False" SelectedIndex="0">
<DataGrid.Columns>
<DataGridTextColumn Header="File" Binding="FileName" />
<DataGridTextColumn Header="Size" Binding="Size" Width="10" />
</DataGrid.Columns>
</DataGrid>
Keyboard Shortcuts
| Key | Action |
|---|---|
| Up Arrow | Move selection to the previous row. |
| Down Arrow | Move selection to the next row. |
| Home | Move selection to the first row. |
| End | Move selection to the last row. |
Key Concepts
DataGridextendsSelector, inheritingSelectedIndex,SelectedItem, andSelectionChangedevent.- Columns use a WPF-aligned type hierarchy:
DataGridColumn→DataGridBoundColumn→DataGridTextColumn/DataGridCheckBoxColumn, andDataGridColumn→DataGridTemplateColumn. - The
Bindingproperty on bound columns is a string path resolved viaPropertyAccessorRegistry— fully Native AOT compatible, no reflection at runtime. - Data item types must be discoverable by the source generator: use
[BindableObject]for plain records/classes, or extendViewModelBase. - A
Widthof0uses star sizing, filling all remaining horizontal space equally. - Sort indicators
▲(ascending) and▼(descending) are rendered in the header next to the column title. DataGridTemplateColumnrenders each cell via aCellTemplate, settingDataContexton the template content to the row's data item.