So I have a WPF application and a datagrid in it, uneditable, with full row selecting enabled, and I'm trying to make a kind of toggle on-off functionality with the datagrid rows. However, I can't seem to find the appropriate Event for a simple row selection. There's SelectionChanged
, which doesn't work if I click again on the already selected item. There's simple Click
(many types of them), but all of them happen BEFORE the row is actually selected, so if I try to get selected item on the click I will get null. There is some other Event (that I forgot about) that requires to click twice, which is not really what I want. I'm running out of ideas, maybe there's some good event-combination or some way to override them or maybe I'm just missing something?

- 160,644
- 26
- 247
- 397

- 171
- 8
-
2Please don't prefix your titles with "C#" and such. That's what the tags are for. – John Saunders May 15 '12 at 20:03
3 Answers
Considering the information you have given, @Yatrix's solution is perfectly valid!
But to that you have responded ..
Happens before the row is selected, so doesn't work :/
Then there is something you are missing here. Even if LeftMouseDown \ LeftMouseUp \ PreviewLeftMouseDown \ Up events occur before selection event, they would know if the row is already selected or not. That way they can deselect it and then do
e.Handled=true
.... so that selection is never called after mouse events thus the reselection is avoided.
Try and let me know.

- 19,625
- 8
- 55
- 71
PreviewMouseDown
or PreviewMouseLeftButtonDown
may help. I also found this on this site that may provide you with direction:
How can I get a DataGrid to unselect on click when SelectionMode="Extended"?
If you want to sign up for the row Selected event you'll need to do it for each row in the DataGrid. Try signing up for the LoadingRow event on the DataGrid and for each row sign up for the Selected event.

- 3,723
- 28
- 36