简体   繁体   English

如何将WPF Datagrid项目源绑定到集合集合?

[英]How to bind WPF Datagrid itemssource to Collection of Collections?

What is the best approach to bind a WPF DataGrid ItemsSource to an ObservableCollection of ObservableCollections ? 将WPF DataGrid ItemsSource绑定到ObservableCollections的ObservableCollection的最佳方法是什么?

eg 例如

public ObservableCollection<ObservableCollection<MyDataItem>> DataValues = new ObservableCollection<ObservableCollection<MyDataItem>>();

where MyDataItem may look like this: MyDataItem可能如下所示:

public class MyDataItem
{
    public string Caption { get; set; }
    public string DataValue { get; set; }
}

I can assume the collection of collections isn't jagged, and they all contain the same number of "columns" 我可以假设集合的集合不是锯齿状的,并且它们都包含相同数量的“列”

Is it possible to bind each column dynamically to the 'DataValue' property of the MyDataItem objects or do I need to pack the data into an easier structure to bind to? 是否可以将每列动态绑定到MyDataItem对象的'DataValue'属性,还是我需要将数据打包到一个更容易绑定的结构中?

This is possible by using a collection-derived class as the inner object, and implementing ICustomTypeDescriptor on it - See a similar SO question on the topic (tagged with Silverlight, but same idea) 这可以通过以下方式实现:将集合派生的类用作内部对象,并在其上实现ICustomTypeDescriptor参见该主题的类似SO问题(标记为Silverlight,但思路相同)

Silverlight DataGrid - Binding to a Collection of Collections of objects. Silverlight DataGrid-绑定到对象的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM