DataTemplating数据嵌套ObservableCollections的结合 [英] DataTemplating for data binding of nested ObservableCollections

查看:139
本文介绍了DataTemplating数据嵌套ObservableCollections的结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立其通过绑定到一个ObservableCollection,它本身包含另一个的ObservableCollection从我需要在ListView中显示的数据填充ListView控件。

I am attempting to create a ListView control which is populated via a binding to an ObservableCollection, which itself contains another ObservableCollection from which I need to display data in the ListView.

要澄清我有一个对象的层次结构如下:

To clarify I have an object hierarchy as follows:

ClassA
{
    int ID = 123;
    string Name = "Value";
}

ClassB
{
    int ID = 456;
    ObservableCollection<ClassA> CollectionOfClassA;
}

ClassC
{
    int ID = 789;
    ObservableCollection<ClassB> CollectionOfClassB;
}

在此,我想用下面的ListView布局结束:

From this I would like to end up with the following ListView layout:

(ClassC.CollectionOfClassB ListView)
+-------------+-------------+------------------+--------------------------------+
|  ClassB.ID  |  CollectionOfClassA.ClassA.ID  |  CollectionOfClassA.ClassA.ID  |
+-------------+-------------+------------------+--------------------------------+
|  ClassB.ID  |  CollectionOfClassA.ClassA.ID  |  CollectionOfClassA.ClassA.ID  |
+-------------+-------------+------------------+--------------------------------+

要做到这一点,我将承担我需要我的ListView绑定到我的收藏CollectionOfClassB但是,DataTemplate中的结构和后续生产的行绑定有我有点为难。

To achieve this I would assume I need to bind my ListView to my CollectionOfClassB collection, however, the datatemplate structure and subsequent binding for producing the rows has me a bit stumped.

谁能帮我这个好吗?

非常感谢,

编辑:

在上面的ID字段引用为ClassC.CollectionOfClassB集合中的索引对象表中的第一列中,每行被存储在CollectionOfClassB单个对象

The first column in the table above references the ID field for the indexed object in the ClassC.CollectionOfClassB collection, each row being an individual object stored in the CollectionOfClassB.

每个存储在CollectionOfClassB的ClassB的对象都有自己的CollectionOfClassA,这我试图在ListView放置在同一行是其父母。每个ClassA的对象CollectionOfClassA是一列。

Each of the ClassB objects stored in CollectionOfClassB has its own CollectionOfClassA, which I am attempting to place in the listview on the same row as its parent. Each ClassA object in CollectionOfClassA being a column.

编辑2:

第一列从ClassB的ID字段在CollectionOfClassB当前项目填充,随后的列填充从CollectionOfClassA每个项目的数据。列标题是ClassA.ID,而内容将ClassA.Name例如

The first column is populated by the ID field from ClassB for the current item in the CollectionOfClassB, the subsequent columns are populated with data from each item in the CollectionOfClassA. The column header would be ClassA.ID, whilst the content would be ClassA.Name for example.

我要找例如XAML标记,让我实现这一目标。

I am looking for example XAML markup that will allow me to achieve this.

推荐答案

如果您知道 ClassB.CollectionOfClassA 总是相同的长度,可以绑定到一个索引值它

If you know that ClassB.CollectionOfClassA is always the same length, you can bind to an indexed value of it

下面是一个粗略的例子

<!-- Assumes DataContext is ClassC -->
<ListView ItemsSource="{Binding CollectionOfClassB}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="ClassB.Id" />
            <GridViewColumn DisplayMemberBinding="{Binding CollectionOfClassA[0].Id}" Header="ClassA[0].Id" />
            <GridViewColumn DisplayMemberBinding="{Binding CollectionOfClassA[1].Id}" Header="ClassA[1].Id" />
        </GridView>
    </ListView.View>
</ListView>

如果您有列的动态数量,它成为一个有点棘手

If you have a dynamic number of columns, it becomes a bit trickier

最简单的方法是使用自定义列并显示东西的项目,如水平的StackPanel

The simplest way would be to use a custom column and display the items in something like a horizontal StackPanel

<!-- Assumes DataContext is ClassC -->
<ListView ItemsSource="{Binding CollectionOfClassB}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="ClassB.Id" />
            <GridViewColumn Header="CollectionOfA"
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding CollectionOfClassA}">
                            <ItemsControl.ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsControl.ItemsPanelTemplate>

                            <ItemsControl.ItemTemplate>
                                <TextBlock Text="{Binding Id}" Width="50" />
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
        </GridView>
    </ListView.View>
</ListView>

当然,这不使用内置的GridView控件功能,如排序

Of course, this doesn't use the built-in GridView features like sorting

这篇关于DataTemplating数据嵌套ObservableCollections的结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆