简体   繁体   English

XAML绑定到动态嵌套对象

[英]XAML Binding to dynamic nested objects

I have a response from a JSON API and map it in my Code to an Object. 我收到JSON API的响应,并将其在我的代码中映射到对象。 The Object Structure looks like this: 对象结构如下所示:

public class A {
    public B b;
    public C c;
    public D d;
}
public class B{
    public string a;
    public string b;
}
...
public class D{
    public F[] f;
}
public class F{
    public string c;
    public string d;
    public string e;
}

I set up my Binding to an Observable Collection <A> aCollection and it's working fine. 我将绑定设置为Observable Collection <A> aCollection ,并且工作正常。 I have some Objects of A and can access all members like this. 我有一些A对象,并且可以像这样访问所有成员。

<ListView ItemsSource="{Binding aCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding b.a, TargetNullValue='-'}"/>
                        <Label Text="{Binding c.a, TargetNullValue='-'}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

But I don't know how I can access the Array F[] and the members of F. Also I don't know in advance how large the array will be. 但是我不知道如何访问数组F []和F的成员。而且我也不预先知道数组将有多大。

What I want to achieve is to display every entry in the array in relation to the containing Object A. 我要实现的是显示数组中与包含的对象A有关的每个条目。

Hope this question makes sense. 希望这个问题有意义。

If you want to bind Array data into every cell in listview .According to your code you can modify like this: 如果您要将Array数据绑定到listview中的每个单元格中,请根据您的代码进行如下修改:

<ListView ItemsSource="{Binding aCollection}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding b.a, TargetNullValue='-'}"/>
                    <Label Text="{Binding c.a, TargetNullValue='-'}"/>

                    //***
                        <ListView ItemsSource="{Binding d.F}">
                          <ListView.ItemTemplate>
                             <DataTemplate>
                               <ViewCell>
                                  <StackLayout>
                                     <Entry Text="{Binding c}"/>
                                     <Entry Text="{Binding d}"/>
                                     <Entry Text="{Binding e}"/>
                                  </StackLayout>
                               </ViewCell>
                             </DataTemplate>
                          </ListView.ItemTemplate>
                        </ListView>
                    //***

                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

use listview contain a listview to show data etc.And your class D change to 使用listview包含一个listview来显示数据等,并且您的D类更改为

 public class {
    public List<F> f{ get; set; }
 }

Not sure whether is you want,hope to be useful. 不确定您是否想要,希望有用。

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

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