R – WPF DataGrid sort by ComboBox field

comboboxdatagridsortingwpf

I have WPF datagrid with combox column (ID is real value, Desc is displayed value) and when I click on header of that column automatically sorts by real value (ID). I want to sort by displayed value.

My WPF datagrid has 4 columns: IdPerson, DescSchool, IdSchool and School. Column "School" is comboBox with this values:ItemSource = schoolTable.DefaultView, SelectedValueBinding = new Binding("IdSchool"), SelectedValuePath="IDSchool", DisplayMemberPath = "DescSchool"

schoolTable is a table with 2 columns – IDSchool and DescSchool. That table is used just as datasource for combobox.

I tried the solution when I have set SortMemberPath = "DescSchool" and initially, this works – when I click on the header of the combobox column sorting is done by displayed value (because it read value of the other column) and not by real value. But, if I change the value of the combobox, value of the column "DescSchool" is still the same so after that sorting doesn't work anymore properly.

Any idea?

Best Answer

Setting SortMemberPath="Desc" (or what your property is called) on the DataGridComboBoxColumn should do the trick.