簡體   English   中英

XAML綁定到動態嵌套對象

[英]XAML Binding to dynamic nested objects

我收到JSON API的響應,並將其在我的代碼中映射到對象。 對象結構如下所示:

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;
}

我將綁定設置為Observable Collection <A> aCollection ,並且工作正常。 我有一些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>

但是我不知道如何訪問數組F []和F的成員。而且我也不預先知道數組將有多大。

我要實現的是顯示數組中與包含的對象A有關的每個條目。

希望這個問題有意義。

如果您要將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>

使用listview包含一個listview來顯示數據等,並且您的D類更改為

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

不確定您是否想要,希望有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM