Ernestas Romeika
Ernestas Romeika

Reputation: 171

Chosing datagrid event

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?

Upvotes: 0

Views: 221

Answers (3)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

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.

Upvotes: 2

Dan Busha
Dan Busha

Reputation: 3803

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.

Upvotes: 0

Yatrix
Yatrix

Reputation: 13775

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"?

Upvotes: 0

Related Questions