且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-04 02:45:45

你的类代码看起来不错,现在只是一个将结果束缚于观点的事项。您正在返回对象列表,因此您应该使用支持显示多个项目的控件。当然,您可以将所有图书的id连接到一个字符串,并将其显示在标签中。但这不是怎么做的。您应该做的是将一个ListBox控件添加到XAML并在其中创建一个DataTemplate。这样,您可以设置项目的显示方式。

Your class-code looks good so now it's just a matter of binding the results to the view. You are returning a list of objects so you should use a control that supports showing multiple items. Of course you could concatenate all the id's of the books to one string and show it in a label. But this is not how it's done. What you should do is add a ListBox control to the XAML and create a DataTemplate inside it. This way you set the way items will be displayed.

创建一个类,将作为您的XAML页面的ViewModel。该类将具有属性地方(类型:列表< Place> )。在获取所有数据完成后的OnNavigatedTo事件中,填写ViewModel并将其绑定到XAML的DataContext:

Create a class that will be the ViewModel for your XAML-page. This class will have a property 'Places' (type: List<Place>). In the OnNavigatedTo-event when getting all the data is done, fill the ViewModel and bind and bind it to the DataContext of the XAML:

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

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

This way you can grab all the objects from the ViewModel in your XAML:

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