【问题标题】:How to bind a datagrid column combobox to a second-level list如何将数据网格列组合框绑定到二级列表
【发布时间】:2014-04-27 08:00:57
【问题描述】:

听到的是问题,我有一个类,即“所有者”,它有一个名为“动物”的另一个类的列表。现在我想在网格中显示所有者,其中包含反映他们动物的组合列。

下面是代码(我尽量简单):

数据网格的 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;
    }

【问题讨论】:

    标签: c# wpf list datagrid combobox


    【解决方案1】:

    您必须在 EditingElementStyleElementStyle 上设置 ItemsSource 值。此外,如果您想在组合框中显示 AnimalName,请将 DisplayMemberPath 设置为 Animal

    <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>
    

    更新

    如果你想保留组合框的选择,你需要在 Owner 类中有一个 SelectedAnimal 属性并绑定 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>
    

    【讨论】:

    • 谢谢,它现在会填充,但当我离开单元格时不会留下!
    • 为此,您必须绑定到 Owner 类中的某些属性。请参阅答案中的更新。
    • 谢谢,它现在可以工作了 :) 但我的名声不允许我投票给你 :(
    • 没问题@mesmoll。乐意效劳..!! :)
    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2017-12-08
    • 2014-03-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多