2014-04-27 104 views
0

聽到是問題,我有一個類,即「Owner」,它具有名爲「Animal」的另一個類的列表。現在我想用一個反映他們動物的組合欄向網格中顯示業主。如何將DataGrid列組合框綁定到二級列表

下面是代碼(I使它儘可能簡單):

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

} 

Animal類:

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

而負載事件如下:

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

你必須設置EditingElementStyleElementStyleItemsSource值。另外如果你想顯示在組合框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> 

UPDATE

如果你想保留ComboBox控件的選擇,你需要有一個SelectedAnimal property Owner and bind SelectedItem該屬性的comboBox屬性。

所有者類:

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

謝謝,現在將填充,但是當我離開細胞不會留! – mesmoll

+0

爲此,您必須綁定到Owner類中的某個屬性。請參閱答案中的更新。 –

+1

謝謝,它現在正在工作:) 但我的聲望不會讓我投票給你:( – mesmoll