Windows Phone 8 上 ListBox 中的数据绑定 [英] Data binding in ListBox on Windows Phone 8

查看:24
本文介绍了Windows Phone 8 上 ListBox 中的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Windows Phone 8 的新手.我有一个来自服务器的数据列表,格式如下:

I'm new to Windows Phone 8. I have a list of data from a server in this form:

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

我想在 UI 的 ListBox 中显示该数据:

I want to display that data in a ListBox in the UI:

  <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,比如这个:

The ItemsSource on the ListBox should be bound to friends, like this:

<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):

And also if you haven't done it already, you need to set the DataContext for the page after you downloaded the data, like this (assuming you do the downloading in the code-behind file of a page, e.g. MainPage.xaml.cs):

RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
this.DataContext = json;

这篇关于Windows Phone 8 上 ListBox 中的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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