c# - 使某些行在 ViewModel 的 DataGrid 中不可选择

标签 c# wpf xaml mvvm wpfdatagrid

我正在按照 MVVM 模式构建 WPF 客户端。客户端使用一个wpf DataGrid 来显示一些具有一些复杂分组需求的数据。我没有尝试找到一种方法来在网格中显示所有这些分组,而是简单地为组页眉和页脚生成合成条目。

所有这些工作正常,但它给我留下了一个问题,我的 DataGrid 中有一些行不应该是可选的。我希望我可以控制 View 模型中的选择,但这似乎不起作用。我创建了一个示例项目来说明问题。

App.xaml

<Application x:Class="TestMVVM.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:TestMVVM.ViewModel"
         StartupUri="MainWindow.xaml">
  <Application.Resources>
    <ResourceDictionary>
        <vm:MainViewModel x:Key="MainViewModel"/>
    </ResourceDictionary>
  </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="TestMVVM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" DataContext="{StaticResource MainViewModel}">
  <Grid>
    <DataGrid ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem}" IsSynchronizedWithCurrentItem="True"
              AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow">
        <!-- In the real app I use Style triggers here to highlight title and total rows and make them unselectable from the mouse,
             but you can still select them via the keyboard and when the grid is initially displayed it's on an invalid row -->
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Count" Binding="{Binding Count}"/>
        </DataGrid.Columns>
    </DataGrid>
  </Grid>
</Window>

MyItem.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestMVVM.Model
{
  public class MyItem
  {
    public MyItem(string name, int count)
    {
        Name = name;
        Count = count;
    }

    public string Name { get; private set; }
    public int Count { get; private set; }
  }

  public class MyItemTitle
  {
    public MyItemTitle(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
  }

  public class MyItemTotal
  {
    public MyItemTotal(string name, int total)
    {
        Name = name;
        Count = total;
    }

    public string Name { get; private set; }
    public int Count { get; private set; }
  }
}

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using TestMVVM.Model;

namespace TestMVVM.ViewModel
{
  public class MainViewModel : INotifyPropertyChanged
  {
    private object _mySelectedItem;
    private int _lastIndex = -1;

    public MainViewModel()
    {
        var myItems = new List<object> {
            new MyItemTitle("Top Title"),
            new MyItemTitle("Subtitle"),
            new MyItem("Real Item", 1),
            new MyItem("Second Real Item", 4),
            new MyItemTotal("Subtitle totals", 5),
            new MyItem("Third Real Item", 11),
            new MyItemTotal("Top Title Totals", 16)
        };

        // Initially I used a List<> here but I thought maybe ObservableCollection
        // might make a difference.  As you can see, it does not.
        MyItems = new ObservableCollection<object>(myItems);
    }

    public IList<object> MyItems { get; private set; }

    public object MySelectedItem
    {
        get { return _mySelectedItem; }
        set
        {
            SetValidItem(value);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void SetValidItem(object value)
    {
        var newIndex = MyItems.IndexOf(value);

        if (newIndex == _lastIndex) return;  // no change, doubt this can happen

        if (IsItemValid(value))
        {
            _mySelectedItem = value;
            _lastIndex = newIndex;

            return;                         // selection worked as DataGrid expected
        }

        if (newIndex > _lastIndex)          // selection going down the grid
        {
            for (var i = newIndex + 1; i < MyItems.Count; ++i)
            {
                if (IsItemValid(MyItems[i]))
                {
                    _mySelectedItem = MyItems[i];
                    _lastIndex = i;
                }
            }
        }
        else if (newIndex < _lastIndex)    // selection going up the grid
        {
            for (var i = newIndex - 1; i > -1; --i)
            {
                if (IsItemValid(MyItems[i]))
                {
                    _mySelectedItem = MyItems[i];
                    _lastIndex = i;
                }
            }
        }

        // three possible scenarios when we get here:
        //   1) selection went up higher than selected in grid
        //   2) selection went down lower than selected in grid
        //   3) no valid higher/lower item was found so we keep the previous selection
        // in any of those cases we need to raise an event so the grid knows
        // that SelectedItem has moved

        RaiseOnMySelectedItemChanged();

        // I checked in the debugger and the event fired correctly and the DataGrid
        // called the getter again with the correct value.  The grid seemed to ignore
        // it though so I thought I could force the issue with this.  This doesn't seem
        // to do anything either (I even tried collectionView.MoveCurrentToLast())
        var collectionView = CollectionViewSource.GetDefaultView(MyItems);
        collectionView.MoveCurrentTo(_mySelectedItem);
    }

    private bool IsItemValid(object value)
    {
        return value is MyItem;
    }

    private void RaiseOnMySelectedItemChanged()
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            var args = new PropertyChangedEventArgs("MySelectedItem");

            handler(this, args);
        }
    }
  }
}

最佳答案

1. 解决此问题的一种方法是扩展 DataGrid。然后在 DataGridExtended 中确定是否需要从选择中跳过某些 RowCell(可能将焦点移动到下一个单元格或行) .处理部分很简单。更难的是获取单元格或行。这是一篇如何做到这一点的帖子: How to get a cell from DataGrid?

在您的 ViewModel 或模型中创建将项目标记为不可选择的属性,并在 DataGridExtended 中响应它。

2. 还有一篇关于该主题的有趣帖子可能对您有用,或者是额外的帮助。它基本上创建了一种样式,使该行无法聚焦。 Making a row non-focusable in a WPF datagrid

3.可以在后台代码中直接响应和取消选择事件。与(第 2 部分)一起,这可能会成功。这是另一个如何开始的链接:http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing&referringTitle=Tips%20%26%20Tricks

关于c# - 使某些行在 ViewModel 的 DataGrid 中不可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20922787/

相关文章:

c# - 将自定义依赖属性添加到 XAML 中的控件模板

c# - Xamarin.iOS 状态栏在更改其文本颜色时消失

c# - 最佳实践 : efficient sprite drawing in XNA

c# - WPF 中的高级/复杂快捷方式

c# - 实时可视化树中缺少动态添加的控件

c# - XamlParseException 是未处理的 C# 应用程序

c# - 如何在C#中向visual svn server 添加权限条目

c# - 应用程序未调用方法

具有 Storyboard 动画的 WPF 用户控件不稳定/不稳定

c# - 将按钮 XAML 更改为 C#