【问题标题】:Prevent users from changing DataGrid's SelectedItem防止用户更改 DataGrid 的 SelectedItem
【发布时间】:2011-09-24 11:44:54
【问题描述】:

我有一个 WPF 4.0 应用程序,我正在使用 WPF DataGrid。我想要的是使用视图上的导航按钮来更改 DataGrid 中的 SelectedItem,而不是让用户通过单击 DataGrid 来更改 SelectedItem。我在 ViewModel 中使用 ICollectionView 作为 DataGrid 的 ItemsSource。这是我目前所拥有的:

NextCommand (ViewModel):

DefaultView.MoveCurrentToNext();  // DefaultView is an ICollectionView
SelectedItem = DefaultView.CurrentItem as MyProperty;

数据网格(视图):

<DataGrid ItemsSource="{Binding Path=DefaultView}"
          SelectedItem="{Binding Path=SelectedItem}"
          IsSynchronizedWithCurrentItem="True">
    ...
</DataGrid>

导航按钮很好用……但是,我不想让用户单击 DataGrid 来更改 SelectedItem。关于如何做到这一点的任何想法?我玩过 DataGrid_SelectionChanged 事件,但问题是 SelectedItem 上的绑定会在此事件触发之前更新 ViewModel。我希望 SelectedItem 不会更改两次(用户单击时一次,将其设置回原始值时两次)。如果需要,我可以使用后面的代码...

【问题讨论】:

    标签: wpf mvvm datagrid


    【解决方案1】:

    显然您必须通过自定义 DataGrid 模板禁用它:

    http://www.wpfsharp.com/2011/05/how-to-disable-row-selection-in-a-wpf-datagrid/

    出于好奇,您为什么要这样做?我的意思是你为什么要禁止用户通过点击选择一行,却允许用户通过使用一些导航按钮间接选择一行?

    【讨论】:

    • 我会看看这个。我正在使用 DataGrid 来显示需要按特定顺序发生的步骤列表。当这些步骤完成时,它们将被标记为已完成。我不希望用户以错误的顺序完成这些步骤。
    猜你喜欢
    • 2014-08-26
    • 2018-07-18
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2012-10-28
    • 2013-04-25
    相关资源
    最近更新 更多