Jivlain
Jivlain

Reputation: 3658

Preserve selection when sorting a WPF DataGrid

When sorting a WPF datagrid, the standard behaviour is for the selected item to be lost. Using the WinForms DataGridView it was possible to restore the selected item by hooking the CellMouseDown event, but the WPF DataGrid doesn't have one of those.

It is also possible to preserve the sort order by providing custom sort logic in the Sorting event, but is there any way of preserving the current selection while using the default sort logic?

Clarification: The sort happens when the user clicks the column header.

Upvotes: 0

Views: 340

Answers (1)

Noctis
Noctis

Reputation: 11783

If you have a unique field on that grid, save it before the call to the sort method, sort, and then set the selected item to the object that has that unique field ...

Upvotes: 0

Related Questions