如何将数据绑定到Xaml,Windows Phone?

时间:2013-01-04 08:32:24

标签: json windows-phone-7 xaml data-binding

这是我的C#代码,现在它在控制台中显示数据。

class PlaceViewModel
    {

        public List<Vantaa.IntroPage.Place> Places;
    }       

public class Place
            {
                public string id { get; set; }
                public string title { get; set; }
                public string latitude { get; set; }
                public string longitude { get; set; }
                public string www { get; set; }
            }

        public class RootObject
        {
            public List<Place> Places { get; set; }
        }

    protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place"));
        }

        private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);

            foreach (var book in rootObject.Places)
            {
                System.Diagnostics.Debug.WriteLine(book.id);
            }
            this.DataContext = new PlaceViewModel
                 {
                      Places = rootObject.Places
                 };
            }

我应该怎样处理xaml文件才能在textblock中显示数据?

这是我目前的xaml代码。它肯定不起作用。我真的不知道。

<ListBox ItemsSource="{Binding Places}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Path=id}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

2 个答案:

答案 0 :(得分:5)

你的类代码看起来不错,所以现在只需将结果绑定到视图。您将返回一个对象列表,因此您应该使用支持显示多个项目的控件。当然,您可以将书籍的所有ID连接到一个字符串并在标签中显示。但这不是它的完成方式。你应该做的是将一个ListBox控件添加到XAML并在其中创建一个DataTemplate。这样就可以设置项目的显示方式。

创建一个将成为XAML页面的ViewModel的类。此类将包含“Places”属性(类型:List<Place>)。在完成所有数据的OnNavigatedTo事件中,填充ViewModel并绑定并将其绑定到XAML的DataContext:

this.DataContext = new YourViewModel { Places = rootObject.Places };

这样您就可以从XAML中的ViewModel中获取所有对象:

<ListBox ItemsSource="{Binding Places}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Text="{Binding Path=id}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

修改

这是一个有效的例子:

<强> XAML:

<Grid>
    <ListBox ItemsSource="{Binding Places}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=ID}" />
                    <TextBlock Text="{Binding Path=Title}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

<强> Place.cs:

public class Place
{
    public string ID { get; set; }
    public string Title { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Web { get; set; }
}

<强> MainViewModel.cs:

public class MainViewModel
{
    public MainViewModel()
    {
        Places = new List<Place>();
    }

    public List<Place> Places { get; set; }
}

示例存储库(您应该拥有自己的存储库):

public static List<Place> FetchData()
{
    var lst = new List<Place>();

    lst.Add(new Place { ID = "1", Title = "One", Latitude = "111", Longitude = "111", Web = "www......" });
    lst.Add(new Place { ID = "2", Title = "Two", Latitude = "222", Longitude = "222", Web = "www......" });
    lst.Add(new Place { ID = "3", Title = "Three", Latitude = "333", Longitude = "333", Web = "www......" });
    lst.Add(new Place { ID = "4", Title = "Four", Latitude = "444", Longitude = "444", Web = "www......" });

    return lst;
}

<强> MainWindow.xaml.cs:

public MainWindow()
{
    InitializeComponent();
    //This is where the magic happens
    //Fill the viewModel with the data
    var viewModel = new MainViewModel { Places = Repository.FetchData() };
    //Assign the viewModel with the data to the DataContext
    //The bindings will be automatically done in the XAML
    DataContext = viewModel;
}

答案 1 :(得分:2)

您的PlaceViewModel类应实现INotifyProprtyChanged接口,并且您要绑定的所有属性都应在setter中通知它们的更改。

为了能够更新绑定文本块中的值,属性可以通知其更改。

实施例: http://msdn.microsoft.com/en-us/library/ms229614.aspx