Datagrid SelectedItem lost on window.IsEnabled = false

I have a (main) Window containing a Frame . A Page loaded into this Frame , and some viewmodel is used as its datacontext.

There are several datagrids in the view that are attached to the viewmodel, and the other is attached to the selected item (so that you get a view of the main details).

The problem occurs when I pop up a popup and set mainwindow IsEnabled = false. When this happens, the selected item from the datagrid tied to the viewmodel will not be selected, and, of course, the other selected data set items will also not be selected.

How can I save the "state" of the user interface when my mainwindow.IsEnabled gets false?

+4
source share
3 answers

If you want to add some kind of madness to the mix. After installing VS 2011 Beta (which replaces the .NET 4.0.NET.NET platform), I created a small test application with VS 2010 that targets 4.0 , and its behavior has changed again so as not to set SelectedItem = null when the grid is disconnected. I do not know if this will make the final version 4.5 or not.

+1
source

It seems that the built-in behavior of the datagrid is to deselect the selected item when the datagrid disconnected - just checked it myself.

But try the following:

Create a simplified ListView, add some data, select an item, click the button to disable the ListView. If the selected item is canceled, then this behavior will be performed in WPF. I think you can not help it. Also, that you don’t turn off your controls ...

If the selected item is not canceled, you may open an error in Connect. It may take several months to get an answer, and the correction may appear in a couple of years ... If this expectation is not an option, also post an error in the WPF toolkit (wpf.codeplex.com). Toolkit may be updated earlier.

0
source

This is expected behavior by design, but it is unexpected and annoying, and many think it needs to be removed. A workaround is to create a new DataGrid class that inherits from a DataGrid that changed the behavior of this event.

Xaml

 <DataGrid x:Class="MyDataGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"> </DataGrid> 

Code for

 public partial class MyDataGrid : DataGrid { public MyDataGrid() { InitializeComponent(); } static MyDataGrid() { IsEnabledProperty.OverrideMetadata( typeof(MyDataGrid), new FrameworkPropertyMetadata(OnIsEnabledChanged)); } private static void OnIsEnabledChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(CanUserAddRowsProperty); d.CoerceValue(CanUserDeleteRowsProperty); CommandManager.InvalidateRequerySuggested(); } } 

See http://wpf.codeplex.com/workitem/13022

0
source

All Articles