When I update a datagrid SelectedItem from code (via a bound object in a ViewModel), how to I get the visual grid to highlight the newly selected item?
Thanks,
Mark
UPDATE: This is still an issue for me. My SelectedItem property already implements change notification, but the datagrid is not VISUALLY displaying which row has been selected – i.e. it is not getting highlighted.
Best Answer
I guess that you really verified that the
SelectedItem
has changed (you could set theBinding
Mode toTwoWay
temporarily to see if it works the other way round, by clicking the row you should see the highlight andSelectedItem
'sset
-method executed). If yes, verify that you really exactly match the Property Name onPropertyChanged
method invoke. Since you're not typesafe here, you might have made a spelling error. If no, check if your Databinding Attribute is set correctly. Another idea is that you might have changed theDataGrid
's styles or template and now you are missing some of the Visual States. TheDataGridRow
can be styled using a style template. You have three selected states, calledUnfocusedSelected
(probably the right one),NormalSelected
andMouseOverSelected
.You could try to make your own Visual State by using this template:
This is a copy-paste from a good MSDN Article on customizing the DataGrid Styles. You could, for example, modify the
UnfocusedSelected
part of the template and see if you see any change when, for example, adding a red border around it or something.Maybe it's worth a try. I hope that you know how to apply own styles. If not, here is another MSDN Resource.
I know, these are just tips, but maybe helpful at last.