Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF数据网格上获取多个SelectedItems(行)_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何在WPF数据网格上获取多个SelectedItems(行)

C# 如何在WPF数据网格上获取多个SelectedItems(行),c#,wpf,mvvm,C#,Wpf,Mvvm,如何在WPF DataGrid上获取多个选定项(行) 我只能使用SelectedItem属性获取一个选定项 XAML: <Window x:Class="WpfDataGridTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

如何在WPF DataGrid上获取多个选定项(行)

我只能使用SelectedItem属性获取一个选定项

XAML:

<Window x:Class="WpfDataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <DataGrid ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem}">
        </DataGrid>
    </Grid>
</Window>
  public class MainViewModel
  {
    public MainViewModel(IEnumerable<Customer> customers)
    {
      Items = new ObservableCollection<Customer>(customers);
    }

    public ObservableCollection<Customer> Items { get; }

    public Customer SelectedItem { get; set; }
  }


<Window x:Class="WpfDataGridTest.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">
    <Grid>
        <DataGrid ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem}"
                  Name="grid">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" 
                                           CommandParameter="{Binding SelectedItems, ElementName=grid}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>
    </Grid>
</Window>
  public class MainViewModel
  {
    public MainViewModel(IEnumerable<Customer> customers)
    {
      Items = new ObservableCollection<Customer>(customers);
    }

    public ObservableCollection<Customer> Items { get; }

    public Customer SelectedItem { get; set; }

    public ICommand SelectionChangedCommand => _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand<IList>(OnChanged));

    private void OnChanged(IList dataset)
    {
      var selectedItems = dataset.OfType<Customer>();
    }

    private ICommand _selectionChangedCommand;
  }

视图模型:

<Window x:Class="WpfDataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <DataGrid ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem}">
        </DataGrid>
    </Grid>
</Window>
  public class MainViewModel
  {
    public MainViewModel(IEnumerable<Customer> customers)
    {
      Items = new ObservableCollection<Customer>(customers);
    }

    public ObservableCollection<Customer> Items { get; }

    public Customer SelectedItem { get; set; }
  }


<Window x:Class="WpfDataGridTest.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">
    <Grid>
        <DataGrid ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem}"
                  Name="grid">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" 
                                           CommandParameter="{Binding SelectedItems, ElementName=grid}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>
    </Grid>
</Window>
  public class MainViewModel
  {
    public MainViewModel(IEnumerable<Customer> customers)
    {
      Items = new ObservableCollection<Customer>(customers);
    }

    public ObservableCollection<Customer> Items { get; }

    public Customer SelectedItem { get; set; }

    public ICommand SelectionChangedCommand => _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand<IList>(OnChanged));

    private void OnChanged(IList dataset)
    {
      var selectedItems = dataset.OfType<Customer>();
    }

    private ICommand _selectionChangedCommand;
  }
public类主视图模型
{
公共主视图模型(IEnumerable客户)
{
项目=新的可观察收集(客户);
}
公共可观察集合项{get;}
公共客户SelectedItem{get;set;}
}
如果我选择了多行,则选择EdItem stay而不进行更改。 如何获取多个选定项目

  • 使用nuget dependency MvvmLight
  • 使用SelectionChanged事件处理程序,并将SelectedItems发送到VM(感谢)
  • 请参阅下面的代码

    XAML:

    <Window x:Class="WpfDataGridTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <DataGrid ItemsSource="{Binding Items}"
                      SelectedItem="{Binding SelectedItem}">
            </DataGrid>
        </Grid>
    </Window>
    
      public class MainViewModel
      {
        public MainViewModel(IEnumerable<Customer> customers)
        {
          Items = new ObservableCollection<Customer>(customers);
        }
    
        public ObservableCollection<Customer> Items { get; }
    
        public Customer SelectedItem { get; set; }
      }
    
    
    
    <Window x:Class="WpfDataGridTest.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">
        <Grid>
            <DataGrid ItemsSource="{Binding Items}"
                      SelectedItem="{Binding SelectedItem}"
                      Name="grid">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" 
                                               CommandParameter="{Binding SelectedItems, ElementName=grid}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGrid>
        </Grid>
    </Window>
    
      public class MainViewModel
      {
        public MainViewModel(IEnumerable<Customer> customers)
        {
          Items = new ObservableCollection<Customer>(customers);
        }
    
        public ObservableCollection<Customer> Items { get; }
    
        public Customer SelectedItem { get; set; }
    
        public ICommand SelectionChangedCommand => _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand<IList>(OnChanged));
    
        private void OnChanged(IList dataset)
        {
          var selectedItems = dataset.OfType<Customer>();
        }
    
        private ICommand _selectionChangedCommand;
      }
    
    
    
    视图模型:

    <Window x:Class="WpfDataGridTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <DataGrid ItemsSource="{Binding Items}"
                      SelectedItem="{Binding SelectedItem}">
            </DataGrid>
        </Grid>
    </Window>
    
      public class MainViewModel
      {
        public MainViewModel(IEnumerable<Customer> customers)
        {
          Items = new ObservableCollection<Customer>(customers);
        }
    
        public ObservableCollection<Customer> Items { get; }
    
        public Customer SelectedItem { get; set; }
      }
    
    
    
    <Window x:Class="WpfDataGridTest.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">
        <Grid>
            <DataGrid ItemsSource="{Binding Items}"
                      SelectedItem="{Binding SelectedItem}"
                      Name="grid">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" 
                                               CommandParameter="{Binding SelectedItems, ElementName=grid}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGrid>
        </Grid>
    </Window>
    
      public class MainViewModel
      {
        public MainViewModel(IEnumerable<Customer> customers)
        {
          Items = new ObservableCollection<Customer>(customers);
        }
    
        public ObservableCollection<Customer> Items { get; }
    
        public Customer SelectedItem { get; set; }
    
        public ICommand SelectionChangedCommand => _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand<IList>(OnChanged));
    
        private void OnChanged(IList dataset)
        {
          var selectedItems = dataset.OfType<Customer>();
        }
    
        private ICommand _selectionChangedCommand;
      }
    
    public类主视图模型
    {
    公共主视图模型(IEnumerable客户)
    {
    项目=新的可观察收集(客户);
    }
    公共可观察集合项{get;}
    公共客户SelectedItem{get;set;}
    公共ICommand SelectionChangedCommand=>\u SelectionChangedCommand??(\u SelectionChangedCommand=new RelayCommand(一旦更改));
    私有void OnChanged(IList数据集)
    {
    var selectedItems=dataset.OfType();
    }
    专用ICommand _selectionChangedCommand;
    }
    
    您可以使用SelectionChanged事件处理程序,将SelectedItems发送到VM。