wpf mvvm在datagrid中显示可观察的集合

我需要在DataGrid显示对象的所有信息

ItemsCollectionAItemsCollectionBItemsCollectionC都是ObservableCollections

我有一个datagrid:

  

这将以网格格式显示所有属性,但ItemsCollectionBItemsCollectionC显示为(集合)。

在此处输入图像描述

如何让ItemsCollectionBItemsCollectionC向下扩展网格并向外展示它们的属性

如果您可以指定列而不是自动生成它们,这可能非常简单。

这是一个例子:

                      

视图模型:

 public class MainWindowViewModel : NotificationObject { public MainWindowViewModel() { Employees = new ObservableCollection { new Employee { EmployeeName = "Steven"}, new Employee { EmployeeName = "Josh"}, }; } public ObservableCollection Employees { get; set; } } 

楷模:

 public class Employee { public Employee() { Dogs = new ObservableCollection { new Dog { Gender = 'M'}, new Dog { Gender = 'F'}, }; Cats = new ObservableCollection { new Cat { Name = "Mitzy" , Kind = "Street Cat"}, new Cat { Name = "Mitzy" , Kind = "House Cat"} }; } public string EmployeeName { get; set; } public ObservableCollection Dogs { get; set; } public ObservableCollection Cats { get; set; } } public class Dog { public char Gender { get; set; } public override string ToString() { return "Dog is a '" + Gender + "'"; } } public class Cat { public string Name { get; set; } public string Kind { get; set; } public override string ToString() { return "Cat name is " + Name + " and it is a " + Kind; } } 

ItemsCollectionA视为Employees ,将ItemsCollectionBItemsCollectionC视为DogsCats 。 它仍然使用ToString来显示我覆盖的Dogs and Cats对象,但是您可以简单地将DataTemplate设置为列中的listBox以决定如何显示模型。 另请注意DataGrid上的AutoGenerateColumns="False"以避免两次创建列。

希望这可以帮助

DataGrid只能管理一个项目源。 一切都是基于行的,列不是那么聪明。

两种方式:

  • 将您的数据组合到具有两组字段的新对象中(将是最简单的)。
  • 并排同步2个数据网格。

好吧,似乎datagrid完全是我在这里需要的错误。 我创建了一个listbox的stackpanel,itemssource设置为每个绑定,它显示正常