Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventing row change in datagrid

I have researched this and am stumped: I have a WPF DataGrid, and using an MVVM model. I want to, under certain circumstances, prevent the ability to change a row in the DataGrid. I have researched this and have tried techniques like the one found here.

In practice, this works, however there is an undesirable 'flicker' (it selects the clicked row for a moment then goes back to the previous selection), while this is a close solution I wish there was a more elegant way such as preventing the row change at all in the first place.

I am surprised there is not a SelectionChanging or BeforeSelectionChanged so I could cancel the event from firing; and forcibly preventing the index change in my view model does not seem to make any difference.

How can I do this?

Thank you.

like image 856
Mani5556 Avatar asked Jul 26 '11 03:07

Mani5556


2 Answers

what happens if you take the previewkeydown and previewmousedown events and just call e.Handled=true under your certain circumstance?

Edit: to satisfy the mvvm style: you can create a Behavior with a DependencyProperty you can bind your circumstance to. in this behavior you can handle the events and maybe some other stuff, like does the user click on a datarow or header...

like image 134
blindmeis Avatar answered Nov 12 '22 14:11

blindmeis


DispatcherPriority has been set to ContextIdle. This makes you have flickering, since your SelectedItem is set twice (and it's been rendered twice). Just set priority to Normal, and you'll have flickering no more.

like image 34
Nihad Rizvanovic Avatar answered Nov 12 '22 15:11

Nihad Rizvanovic