Datagrid's SelectedItem automatically set null when grid control is disabled


selecteditem property automatically set null when i set the grid.IsEnabled = false

file attachments


MaciejSzczepanski wrote Jun 9, 2010 at 8:23 AM

In new version of DataGrid IsEnabledChanged was changed:

private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        //this was added in new version !!!
        if (!(bool)(e.NewValue))

        // Many commands use IsEnabled to determine if they are enabled or not
To solve this issue I've overriden this behaviour in my subclass of DataGrid:

IsEnabledProperty.OverrideMetadata(typeof(MyDataGrid), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsEnabledChanged)));
Where my OnIsEnabledChanged does not call UnselectAllCells();

I have the question what was the reason to add that call in the new version? What are the consequences of overriding it?

wrote Jun 9, 2010 at 8:23 AM

wrote Jun 17, 2010 at 10:19 AM

Victor65 wrote Jun 21, 2010 at 1:19 PM

I have found disappearing of selected item a very annoying change. It is also seriously breaks a lot of my code and will break people’s code too.

I have a number of UI with master-details layout. Before I can select a record in Data Grid and see details bounded to SelectedItem. If I want to edit details I click edit. Data Grid becomes disabled, and detail controls enabled for editing.

Now, when I click edit Data Grid becomes disabled, current selection disappear, and all details records become blank (as being data bound to the null selection in Grid).

So it seems that, instead of disabling DataGrid now I gave to intercept mouse & keyboard events to manually ‘disable Data Grid’, or follow Maciej's suggestion. Both ways are not a good permanent solution for the control of that visibility.

And why do Data Grid need to lose selected item when disabled? List View doesn’t.

Victor65 wrote Jun 21, 2010 at 1:22 PM

Actually I would suggest moving impact of that issue from Low to High, since a lot of folks are using master-detail pattern.

wrote Aug 12, 2010 at 12:25 PM

sagarok wrote Aug 12, 2010 at 12:40 PM

Its so annoying..
Also when DataGrid.IsEnabled back to True, the SelectedItem property doesn refresh self from viewmodel. So MVVM pattern dosnt help to persist selected item.
Is there any workaround to solve this issue without changing that behaviour in a custom datagrid?

wrote Sep 3, 2010 at 8:16 AM

wrote Sep 28, 2010 at 2:57 PM

wrote Oct 12, 2010 at 6:43 PM

wrote Feb 24, 2011 at 5:39 PM

wrote Mar 8, 2011 at 2:27 AM

Discofunk wrote Mar 8, 2011 at 2:30 AM

This completely breaks master / detail view! I'm still on 3.5 + toolkit, did this behavior make it into 4?

ashrafulasif wrote Apr 27, 2011 at 9:59 AM

Its really a big problem of datagrid when using MVVM

wrote May 24, 2011 at 4:26 PM

ashrafulasif wrote Jun 1, 2011 at 10:30 AM

Overriding is not solving the problem. Is there any other way to resolve this problem?

MaciejSzczepanski wrote Jun 1, 2011 at 11:13 AM

It solved the issue for us (We haven't had any issues with that so far), what kind of problems do you have?

wrote Jun 2, 2011 at 5:53 AM

wrote Jun 7, 2011 at 8:11 AM

ashrafulasif wrote Jun 7, 2011 at 8:11 AM

According to your solution it should work, but i am confused that first the base OnIsEnabledChanged and then my override function OnIsEnabledChanged is fired. You can see the attachment for call stack

MaciejSzczepanski wrote Jun 7, 2011 at 11:15 AM

It is important to call overridemetadata before datagrid is used, otherwise both metadata are registered (see: http://msdn.microsoft.com/en-us/library/ms754209.aspx). We override this property on application startup.

ashrafulasif wrote Jun 7, 2011 at 1:38 PM

i am also using it in Static constructor

public class MyGrid: DataGrid
    static MyGrid()
             IsEnabledProperty.OverrideMetadata(typeof(MyGrid), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsEnabledChanged)));

MaciejSzczepanski wrote Jun 7, 2011 at 3:10 PM

@ashrafulasif That's the problem you cannot register it in static constructor, because DataGrids's static constructor will be called before yours. That's why we override it in application startup.

public partial class App : Application
    protected override void OnStartup(StartupEventArgs e)

ashrafulasif wrote Jun 7, 2011 at 4:21 PM

Thanks the provided way is ok to resolve this problem

wrote Jun 19, 2011 at 3:58 AM

Discofunk wrote May 17, 2012 at 3:27 PM

This is still a really annoying behaviour. I suppose this will also make it into WPF 4.5. It would be nice if this behaviour could at least be controlled via property: DataGrid.SelectionToNullOnDisable = false;

It doesn't appear that this ever got onto MS radar.

Discofunk wrote May 17, 2012 at 6:20 PM

Shortly after adding my last comment I ran a test and found that the behaviour has indeed changed in .NET 4.5 - disabling the Grid does NOT set the SelectedItem to null.

However, this opens up a can of worms as .NET 4.5 is not a side-by-side installation but a replacement.

wrote Feb 22, 2013 at 1:40 AM