简体   繁体   English

从ViewModel在DataGrid中使某些行无法选择

[英]Make certain rows unselectable in DataGrid from ViewModel

I am building a WPF client following the MVVM pattern. 我正在构建一个遵循MVVM模式的WPF客户端。 The client uses a wpf DataGrid to display some data with some complex grouping requirements. 客户端使用wpf DataGrid显示一些具有一些复杂分组要求的数据。 Rather than try to find a way to display all this grouping within the grid, I simply generate synthetic entries for group headers and footers. 我没有尝试找到在网格中显示所有这些分组的方法,而是简单地为组页眉和页脚生成合成条目。

All this works ok but it leaves me with the problem that I have rows in my DataGrid that should not be selectable. 这一切都运行正常,但它让我遇到的问题是我的DataGrid中的行不应该是可选择的。 I was hoping I could control the selection from the View Model but that doesn't seem to be working. 我希望我可以从View Model中控制选择,但这似乎不起作用。 I have created a sample project that illustrates the problem. 我创建了一个示例项目来说明问题。

App.xaml 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 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 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 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.One way to approach this is to extend the DataGrid . 1.解决这个问题的一种方法是扩展DataGrid Then in the DataGridExtended determine whether certain Row or Cell needs to be skipped from selection (by maybe moving the focus to the next cell or row). 然后在DataGridExtended确定是否需要从选择中跳过某些RowCell (通过将焦点移动到下一个单元格或行)。 The processing part is easy. 处理部分很简单。 What's harder is to get the cell or the row. 获得单元格或行是更难的。 Here's a post how to do that: How to get a cell from DataGrid? 这是一篇如何做到这一点的帖子: 如何从DataGrid获取单元格?

In your ViewModel or model create properties that mark an item as not-selectable, and respond to it in the DataGridExtended. 在ViewModel或模型中,创建将项标记为不可选的属性,并在DataGridExtended中对其进行响应。

2.There is also an interesting post on the subject that might work for you, or be an additional helper. 2.还有一篇关于这个主题的有趣帖子可能适合你,或者是一个额外的帮手。 It basically creates a style to make the row unfocusable. 它基本上创建了一种样式,使行不可聚焦。 Making a row non-focusable in a WPF datagrid 在WPF数据网格中使行不可聚焦

3.you can directly respond and cancel selection events in code behind. 3.您可以直接回复并取消后面代码中的选择事件。 Together with (part 2.) this might do the trick. 与(第2部分)一起,这可能会成功。 Here's another link how to get started: http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing&referringTitle=Tips%20%26%20Tricks 这是另一个如何入门的链接: http//wpf.codeplex.com/wikipage? title = Single- Click%20Editing& referringTitle = Tips%20%26%20Tricks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM