DataGrid动态列在网格中来自复杂的嵌套对象

时间:2014-01-24 23:52:36

标签: c# wpf mvvm datagrid

我正在使用MVVM编写WPF应用程序,我试图将嵌套的复杂对象绑定到数据网格。我基本上需要动态创建数据网格,我只知道在运行时它还有多少列,还需要从行生成列...

MetaDataDO类,

public class MetaDataDO
{
    public int MetaItemId { get; set; }
    public string Name { get; set; }        
    public int SequenceNumber { get; set; }
    public IList<ItemDO> Volumes { get; set; }
}

ItemDO类,

public class ItemDO
{
    public int ItemId { get; set; }
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }
    public int Period { get; set; }
    public decimal Price { get; set; }
}

在我的视图模型中,我有一个属性ObservableCollection MetaDataCollection,它可以有N个MetaDataDO,每个MetaDataDO都有4个价格。以下示例数据......

MetaDataCollection
1, UK, 10, 
           Volumes
                  1, 24/01/2014 00:00, 24/01/2014 06:00, 1, 100
                  2, 24/01/2014 06:00, 24/01/2014 12:00, 2, 120  
                  3, 24/01/2014 12:00, 24/01/2014 18:00, 3, 110
                  4, 24/01/2014 18:00, 24/01/2014 24:00, 4, 150

2, UAE, 20, 
           Volumes
                  1, 24/01/2014 00:00, 24/01/2014 06:00, 1, 500
                  2, 24/01/2014 06:00, 24/01/2014 12:00, 2, 140  
                  3, 24/01/2014 12:00, 24/01/2014 18:00, 3, 510
                  4, 24/01/2014 18:00, 24/01/2014 24:00, 4, 550
....

我无法控制如何定义这些现有对象,这些对象是由外部系统返回的。一旦我在viewmodel中收到数据,

我想以下列格式在数据网格中的UI上显示结果

  Start          |        End       |  UK | USA
-----------------------------------------------
24/01/2014 00:00 | 24/01/2014 06:00 | 100 | 500 
24/01/2014 06:00 | 24/01/2014 12:00 | 120 | 140 
24/01/2014 12:00 | 24/01/2014 18:00 | 110 | 510 
24/01/2014 18:00 | 24/01/2014 24:00 | 150 | 550 

所以简单来说,我试图以下列方式将属性绑定到radGridView ...

            Start            |             End                  |                                           UK                                                                                            |                       USA
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    MetaDataCollection[0].Volumes[0].Start | MetaDataCollection[0].Volumes[0].End | MetaDataCollection[0].Volumes[0].Price | MetaDataCollection[1].Volumes[0].Price 

    MetaDataCollection[0].Volumes[1].Start | MetaDataCollection[0].Volumes[1].End | MetaDataCollection[0].Volumes[1].Price | MetaDataCollection[1].Volumes[1].Price 

    MetaDataCollection[0].Volumes[2].Start | MetaDataCollection[0].Volumes[2].End | MetaDataCollection[0].Volumes[2].Price | MetaDataCollection[1].Volumes[2].Price 

    MetaDataCollection[0].Volumes[3].Start | MetaDataCollection[0].Volumes[3].End | MetaDataCollection[0].Volumes[3].Price | MetaDataCollection[1].Volumes[3].Price 

如何将实际上是复杂嵌套对象的属性绑定到datagrid以实现此输出格式?

0 个答案:

没有答案