Search code examples
wpfdatagrid

How to bring selected DataGridRow to the top of the DataGrid?


Is there a way to programmatically bring a selected row to the top? I'm thinking of possibly doing a workaround with the ItemsSource.

DataGridRow row = dgMapping.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
row.IsSelected = true;

Solution

  • In the model add a property 'IsSelected' to bind 'IsSelected' property of DataGridRow. In the DataGrid, for ItemSource bind CollectionViewSource with SortDescription for 'IsSelected' property.