I'm using a WPF datagrid in which multiple records can be selected & it can be sorted (by clicking the column headers).
I want to be able to retain the selected rows after the sort.
Please note I can retain the selected row, the problem arises when the rows to retain are more than 1.
Say for example: I select rows with indices 2 & 3, and then sort on a particular column. After the sort functionality I want to be able to retain the selected rows (old indices-2,3) & new indices - 4,5. Thus, after sorting, the 4th & 5th rows must be selected.
The items source for my datagrid is of type ListCollectionView to which I'm adding sort descriptions when reqd to sort.
Things I have tried:
Tried setting the SelectedItems property. Doesn't work since it is a Read Only property.
Tried setting the IsSelected property of the DataGridRow(s), doesn't work. Though I can see the IsSelected property for the selected rows is set to true, on the screen I do not see the records highlighted.
This one may be vague but I set the selected Index multiple times but this only highlights the record for the last selected index.
Each time after sorting, the last selected record is highlighted. Is there a way by which I can retain the selected rows?
EDIT:
I'm now able to set the selected items of the datagrid using Datagrid.SelectedItems.Add(dataGridRow) or setting the IsSelected property on the datagrid row.
The issue is though the selected items are present, they are not highlighted on the screen. How can I get the rows to be highlighted?
For ex: If 3 records were selected & highlighted before sort, after sort, all records are selected (put breakpoints & checked in Code) but only one record is highlighted.