【发布时间】:2020-02-20 14:55:44
【问题描述】:
如何在 WPF DataGrid 上获取多个 Selected Items(行)?
我只能使用 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; }
}
如果我选择多行,则 SelectedItem 保持不变。 如何获取多个选中项?
【问题讨论】:
-
您可以使用 SelectionChanged 事件处理程序,并将 SelectedItems 发送到 VM。