[英]Data binding in ListBox on Windows Phone 8
我是Windows Phone 8的新手。我有來自服務器的以下格式的數據列表:
RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
mylist.ItemsSource = json.friends;
public class Friend
{
public string first_name { get; set; }
public string last_name { get; set; }
public string place { get; set; }
public string going { get; set; }
public string thumbnail { get; set; }
}
public class RootObject
{
public List<Friend> friends { get; set; }
}
我想在用戶界面的ListBox
顯示該數據:
<ListBox x:Name="mylist" Margin="10,0,30,0" Height="486" Width="404" FontSize="20">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="10,0,10,8">
<TextBlock Text="{Binding first_name }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding last_name }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding place }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding going }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding thumbnail }" TextWrapping="Wrap" FontSize="18" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
那是一個工作版本
ListBox
上的ItemsSource
應該綁定到friends
,如下所示:
<ListBox ItemsSource="{Binding friends}" Margin="10,0,30,0" Height="486" Width="404" FontSize="20">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="10,0,10,8">
<TextBlock Text="{Binding first_name }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding first_name }" TextWrapping="Wrap" FontSize="24" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
而且,如果還沒有這樣做,則需要在下載數據后為頁面設置DataContext,如下所示(假設您在頁面的代碼隱藏文件中進行下載,例如MainPage.xaml.cs ):
RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
this.DataContext = json;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.