簡體   English   中英

無法將JSON數組綁定到c#Windows Phone 8.1中的ListBox

[英]Unable to bind JSON Array to ListBox in c# Windows Phone 8.1

大家好,我有一個傑森

public class Contact
{
    public string contact_name_given { get; set; }
    public string contact_name_middle { get; set; }
    public string contact_name_family { get; set; }
    public List<object> contact_phones { get; set; }
    public List<object> contact_addresses { get; set; }
    public List<object> contact_notes { get; set; }
    public string contact_dialer { get; set; }
}
public class RootObject
{
    public string user_uuid { get; set; }
    public string add_date { get; set; }
    public List<Contact> contact { get; set; }
}

我需要綁定到一個列表框:“ contact_name_given”,“ contact_name_family”和“ contact_phones”。 我試圖這樣做:

List<RootObject> des = DeserializeFromJson<List<RootObject>>(result);
for (int i =0; i< des.Count; i++)
PhoneList.ItemsSource = des[i].contact;

但這僅顯示了我的最后一個聯系人姓名,如何顯示列表框中的所有聯系人? 謝謝大家

您應該執行以下操作。

在主列表框的DataTemplate中,您應該再添加3個列表框以顯示信息,這是因為要綁定的屬性是List。

<ListBox Name=“box”>
<ListBox.ItemTemplate>
  <DataTemplate>
    <StackPanel>
        <Listbox ItemSource = {Binding first_property_name_goes_here} >
            <Listbox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    </StackPanel>
                </DataTemplate>
        </Listbox.ItemTemplate>
        </Listbox>


        <Listbox ItemSource = {Binding second_property_name_goes_here} >
            <Listbox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    </StackPanel>
                </DataTemplate>
        </Listbox.ItemTemplate>
        </Listbox>

        <Listbox ItemSource = {Binding third_property_name_goes_here} >
            <Listbox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    </StackPanel>
                </DataTemplate>
        </Listbox.ItemTemplate>
        </Listbox>

</StackPanel>
</DateTemplate>
</ListBox.ItemTemplate>
</Listbox

對於每個嵌套的列表框,您將定義希望項目顯示方式的數據模板,當然,在每個數據模板中,您將綁定列表中類的屬性

暫無
暫無

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

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