WPF DataGrid can't change selected row: stuck on first row
In short, my problem is that the WPF DataGrid won't let me select anything other than the first row. When I first fill it with data, no row is selected. Next, no matter what row I click on, the first row lights up. When I bind the SelectedItem property using OneWayToSource, I see that the correct row was indeed selected. If I choose to click again on a different row, nothing happens: the UI remains stuck on the first row and the SelectedItem property retains the previously correct value. At this point, I have to ctrl-click the first row to deselect things. This allows me to repeat the above situation.
In other words: - the UI seems to be out of sync with what is actually selected. - Ctrl-click is required to de-select the selection. - Ctrl-click must be executed on the first row even if the SelectedItem property indicates that a different row is selected.
I understand this is bizarre behavior. I've tried to duplicate it in a separate project with no success. As such, I'm just throwing this out there to see if anyone has any ideas why it might be acting this way?
The only things that I have not duplicated in my separate project is the use of MEF for the View/ViewModel hookup. Everything else is the same.
EDIT: I just replaced said DataGri开发者_如何学运维d with a ListBox and am experiencing the same problem. I'm using Snoop to try to figure out what might be applied to the control that would change its behavior so much.
My problem was that the objects that I was adding to the DataGrid and ListBox had overridden Equals() and GetHashCode() functions. These really screwed up the way both controls rendered which item was selected, therefore explaining the SelectedItem issue. In the end, all the problems were solved by simply removing/fixing those overrides.
Woot!
精彩评论