SelectedItem 没有属性设置器,这意味着您无法为其分配绑定。但是,我们可以通过 SelectionChanged 事件跟踪多个选择。
假设:您在 ViewModel 中有一个名为 People 的 ObservableCollection。
在 ViewModel 中创建一个名为 SelectedPeople 的 Collection 属性
Public Collection<Person> SelectedPeople { get; set; }
在 XAML 中实现 SelectionChanged 事件。
<DataGrid ItemsSource="{Binding Path=People}" SelectionChanged="Selector_OnSelectionChanged" />
后面代码中的 SelectionChanged 处理程序将如下所示。
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null)
{
e.AddedItems.OfType<Person>()
.ToList()
.ForEach(ViewModel.SelectedPeople.Add);
}
if (e.RemovedItems != null)
{
e.RemovedItems.OfType<Person>()
.ToList()
.ForEach(p => ViewModel.SelectedPeople.Remove(p));
}
}
为简洁起见,我删除了异常管理。
编辑 - 以上是关于处理来自 DataGrid 的多个选择。我已将上面的 PersonModel 重命名为 Person。它是将数据暴露给视图的模型。 ViewModel 是管理模型和用于操作模型的功能的类。如需更多信息,请谷歌MVVM pattern。
关于您最初的问题,处理 DataGridComboBoxColumn 和 DataGrid 之间的绑定,我创建了一个示例。下面的代码可以复制到新项目中。
Person 模型是
public class Person
{
public string FirstName { get; set; }
public string Surname { get; set; }
public string MiddleName { get; set; }
public int Age { get; set; }
}
ViewModel 是
public class MyViewModel
{
public MyViewModel()
{
this.People = new ObservableCollection<Person>()
{
new Person() { FirstName = "A", Surname = "B", MiddleName = "C", Age = 1},
new Person() { FirstName = "D", Surname = "E", MiddleName = "F", Age = 2},
new Person() { FirstName = "G", Surname = "H", MiddleName = "I", Age = 3},
new Person() { FirstName = "J", Surname = "K", MiddleName = "L", Age = 4},
new Person() { FirstName = "M", Surname = "N", MiddleName = "O", Age = 5}
};
this.Items = new ObservableCollection<GridItem>()
{
new GridItem() { Person = this.People[0]},
new GridItem() { Person = this.People[1]},
new GridItem() { Person = this.People[2]},
new GridItem() { Person = this.People[3]}
};
}
public ObservableCollection<Person> People { get; set; }
public ObservableCollection<GridItem> Items { get; set; }
}
View 背后的代码只是创建和公开 ViewModel。
public partial class MainWindow : Window
{
public MainWindow()
{
ViewModel = new MyViewModel();
InitializeComponent();
}
public MyViewModel ViewModel { get; set; }
}
将所有乐趣留在 Xaml 中。
<Window x:Class="StackOverflow._20902950.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:StackOverflow._20902950"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Person" Width="100" DisplayMemberPath="FirstName" SelectedItemBinding="{Binding Path=Person}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}, Path=ViewModel.People}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}, Path=ViewModel.People}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
此命名空间将需要更改为您项目的命名空间。
我希望这会有所帮助。