听到问题,我有一个班级,即"所有者",其中有一个名为" Animal"的另一个班级的列表。现在,我想在一个网格中显示所有者,其中包含一个反映其动物的组合列。
下面是代码(我尽可能简单):
数据网格的XAML代码:
<DataGrid Name="dgvTest" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding OwnerId}" />
<DataGridTextColumn Header="Name" Binding="{Binding OwnerName}"/>
<DataGridComboBoxColumn Header="Animals"
ItemsSource="{Binding Animals}" />
</DataGrid.Columns>
</DataGrid>
所有者类:
public class Owner
{
private int ownerId;
public int OwnerId
{
get { return ownerId; }
set { ownerId = value; }
}
private string ownerName;
public string OwnerName
{
get { return ownerName; }
set { ownerName = value; }
}
private List<Animal> animals;
public List<Animal> Animals
{
get { return animals; }
set { animals = value; }
}
}
动物类:
public class Animal
{
private int animalId;
public int AnimalId
{
get { return animalId; }
set { animalId = value; }
}
private string animalName;
public string AnimalName
{
get { return animalName; }
set { animalName = value; }
}
}
Load事件如下:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<Owner> owners = new List<Owner>();
Animal a1 = new Animal();
a1.AnimalId = 1;
a1.AnimalName = "Dog";
Animal a2 = new Animal();
a2.AnimalId = 2;
a2.AnimalName = "Cat";
Owner o1 = new Owner();
o1.Animals = new List<Animal>();
o1.Animals.Add(a1);
o1.Animals.Add(a2);
o1.OwnerId = 1;
o1.OwnerName = "John";
Animal a3 = new Animal();
a3.AnimalId = 3;
a3.AnimalName = "Mouse";
Animal a4 = new Animal();
a4.AnimalId = 4;
a4.AnimalName = "Sheep";
Owner o2 = new Owner();
o2.Animals = new List<Animal>();
o2.Animals.Add(a3);
o2.Animals.Add(a4);
o2.OwnerId = 2;
o2.OwnerName = "Jennifer";
owners.Add(o1);
owners.Add(o2);
dgvTest.DataContext = owners;
}
答案 0 :(得分:0)
您必须在 ItemsSource
和 EditingElementStyle
上设置 ElementStyle
值。如果您想在comboBox中显示AnimalName
,请将 DisplayMemberPath
设置为动物
<DataGridComboBoxColumn>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Animals}"/>
<Setter Property="DisplayMemberPath" Value="AnimalName"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Animals}"/>
<Setter Property="DisplayMemberPath" Value="AnimalName"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
<强>更新强>
如果您想保留对comboBox的选择,您需要在 SelectedAnimal
类中拥有一个 Owner
属性并绑定 comboBox对该属性的SelectedItem
属性。
在所有者类中:
private Animal selectedAnimal;
public Animal SelectedAnimal
{
get { return selectedAnimal; }
set { selectedAnimal = value; }
}
XAML
<DataGridComboBoxColumn>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Animals}"/>
<Setter Property="DisplayMemberPath" Value="AnimalName"/>
<Setter Property="SelectedItem" Value="{Binding SelectedAnimal}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Animals}"/>
<Setter Property="DisplayMemberPath" Value="AnimalName"/>
<Setter Property="SelectedItem" Value="{Binding SelectedAnimal}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>