Datagrid SelectedItem 在 window.IsEnabled = false 上丢失

发布于 2024-09-24 18:34:54 字数 340 浏览 5 评论 0原文

我有一个(主)Window,其中包含一个FramePage 被加载到该 Frame 中,并使用一些视图模型作为其数据上下文。

视图有一些数据网格,其中一个绑定到视图模型,另一个绑定到 selected.item (以便您获得主详细信息视图..)

当我显示弹出框并设置主窗口时,会出现问题已启用=假。发生这种情况时,绑定到视图模型的数据网格中的选定项目将被取消选择,当然其他数据网格选定的项目也将被取消选择。

当我的 mainwindow.IsEnabled 设置为 false 时,如何保留 UI 的“状态”?

I have a (main)Window containing a Frame. A Page is loaded to that Frame, with some viewmodel as its datacontext.

The View has some datagrids, where one is bound to the viewmodel and the other is bound to the selected.item (so that you get a master-details view..)

the problem occurs when I display a popup box, and set the mainwindow IsEnabled = false. When that happens, the selected item from the datagrid bound to the viewmodel gets unselected, and of course the other datagrids selected items get unselected as well.

How can I preserve the "state" of the UI when my mainwindow.IsEnabled get set to false?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻梦旅人 2024-10-01 18:34:54

如果你想在混合中添加一些疯狂。安装 VS 2011 Beta(用 .NET 4.5 替换 .NET 4.0 框架)后,我使用 VS 2010 面向 4.0 创建了一个小型测试应用程序,其行为再次更改为不设置 SelectedItem = null 当网格被禁用时。我不知道这是否会成为4.5的最终版本......

If you want to add some insanity to the mix. After installing VS 2011 Beta (which replaces the .NET 4.0 framework with .NET 4.5) I created a small test application with VS 2010 which targets 4.0 and the behaviour of this has changed once again to not set SelectedItem = null when the grid is disabled. I don't know if this will make it into the final version of 4.5 or not....

不喜欢何必死缠烂打 2024-10-01 18:34:54

数据网格的内置行为似乎是在数据网格被禁用时取消选择所选项目 - 我自己刚刚验证了这一点。

但试试这个:

创建一个简单的 ListView,添加一些数据,选择一个项目,单击一个按钮以禁用 ListView。如果取消选择所选项目,则此行为是 WPF 中设计的。我认为你对此无能为力。除了不禁用控件之外...

如果所选项目没有取消选择,您可以在 Connect 中打开错误。可能需要几个月的时间才能得到答复,并且“可能”会在几年内修复...如果不能等待,还可以在 WPF 工具包 (wpf.codeplex.com) 中发布错误。该工具包可能会更快更新。

It seems the builtin behaviour of the datagrid is to deselect the selected item when the datagrid gets disabled - just verified this myself.

But try this:

Create a simplistic ListView, add some data, select an item, click a button to disable the ListView. If the selected item gets deselected, then this behaviour is by design in WPF. Not much you can do about it I think. Other than not disabling your controls...

If the selected item does not get deselected you can open a bug in Connect. It might take months to get a reply and a fix "might" come in a couple of years... If such a wait is not an option the also post a bug in the WPF toolkit (wpf.codeplex.com). The toolkit might get updated sooner.

时光清浅 2024-10-01 18:34:54

这是预期的行为,并且是设计使然,但它既出乎意料又令人讨厌,很多人认为应该将其删除。解决方法是创建一个新的 DataGrid 类,该类继承自 DataGrid 并更改了此事件的行为。

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>

代码隐藏

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();
  }
}

请参阅http://wpf.codeplex .com/workitem/13022

This is the expected behaviour, and by design, but it is both unexpected and annoying and a lot of people think it should be removed. The workaround it to create a new DataGrid class that inherits from DataGrid that changed the behaviour 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 Behind

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文