C# 如何在WPF数据网格上获取多个SelectedItems(行)
如何在WPF DataGrid上获取多个选定项(行) 我只能使用SelectedItem属性获取一个选定项 XAML: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">
<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而不进行更改。
如何获取多个选定项目
<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。