c# - 从 MVVM WPF 项目中的 DataGrid 选择多个项目

标签 c# wpf xaml mvvm datagrid

如何从 MVVM WPF 项目的 DataGrid 中选择多个项目?

最佳答案

您可以简单地添加一个自定义依赖属性来做到这一点:

public class CustomDataGrid : DataGrid
{
    public CustomDataGrid ()
    {
        this.SelectionChanged += CustomDataGrid_SelectionChanged;
    }

    void CustomDataGrid_SelectionChanged (object sender, SelectionChangedEventArgs e)
    {
        this.SelectedItemsList = this.SelectedItems;
    }
    #region SelectedItemsList

    public IList SelectedItemsList
    {
        get { return (IList)GetValue (SelectedItemsListProperty); }
        set { SetValue (SelectedItemsListProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemsListProperty =
            DependencyProperty.Register ("SelectedItemsList", typeof (IList), typeof (CustomDataGrid), new PropertyMetadata (null));

    #endregion
}

现在您可以在 XAML 中使用此 dataGrid:

<Window x:Class="DataGridTesting.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:DataGridTesting.CustomDatagrid"
    Title="MainWindow"
    Height="350"
    Width="525">
  <DockPanel>
    <local:CustomDataGrid ItemsSource="{Binding Model}"
        SelectionMode="Extended"
        AlternatingRowBackground="Aquamarine"
        SelectionUnit="FullRow"
        IsReadOnly="True"
        SnapsToDevicePixels="True"
        SelectedItemsList="{Binding TestSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
  </DockPanel>
</Window>

我的 ViewModel:

public class MyViewModel : INotifyPropertyChanged
{
    private static object _lock = new object ();
    private List<MyModel> _myModel;

    public IEnumerable<MyModel> Model { get { return _myModel; } }

    private IList _selectedModels = new ArrayList ();

    public IList TestSelected
    {
        get { return _selectedModels; }
        set
        {
            _selectedModels = value;
            RaisePropertyChanged ("TestSelected");
        }
    }

    public MyViewModel ()
    {
        _myModel = new List<MyModel> ();
        BindingOperations.EnableCollectionSynchronization (_myModel, _lock);

        for (int i = 0; i < 10; i++)
        {
            _myModel.Add (new MyModel
            {
                Name = "Test " + i,
                Age = i * 22
            });
        }
        RaisePropertyChanged ("Model");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged (string propertyName)
    {
        var pc = PropertyChanged;
        if (pc != null)
            pc (this, new PropertyChangedEventArgs (propertyName));
    }
}

我的模型:

public class MyModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

最后,这是 MainWindow 的隐藏代码:

public partial class MainWindow : Window
{
    public MainWindow ()
    {
        InitializeComponent ();
        this.DataContext = new MyViewModel ();
    }
}

我希望这种简洁的 MVVM 设计能有所帮助。

关于c# - 从 MVVM WPF 项目中的 DataGrid 选择多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868445/

相关文章:

c# - 方法在应该返回 'true' 时返回 'false'

c# - Xamarin -> System.Reflection.Assembly.GetExecutingAssembly 不起作用

c# - 如何在播放时比较 MediaElement 的准确位置

wpf - 类型列表的附加属性

c# - 从数据网格中删除项目后重置行号

c# - Windows Phone 8 应用程序动态/以编程方式在 g 网格/面板中创建按钮

c# - Gmail api 读取/解码消息 C#

c# - GetConsumingEnumerable 实际上是否从 BlockingCollection 中删除了一个项目?

c# - ConverterParameter——有什么方法可以传递一些分隔列表吗?

c# - WPF XAML 数据触发器在以编程方式更改属性值 C# 后无法触发