WPF DataGrid add index column

WPF

Assume we have a data grid with a binding to an ItemSource collection. We would like to add a column with the index number.

First, add the AlternationCount binding:


<DataGrid
	...
    ItemsSource="{Binding MyObservableCollection}"
    AlternationCount="{Binding MyObservableCollection.Count}" 
    ... 
    />

Now add the data grid column with binding to the alternation inded


<DataGridTextColumn Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Header="#" FontSize="20"/>

Post a Comment

Previous Post Next Post