Windows Phone 8.1绑定数据会合

Windows Phone 8.1绑定数据会合

Windows Phone 8.1绑定数据集合

Windows Phone的数据绑定可以针对单个对象进行绑定,当然也可以直接绑定大集合而不是单个数据。而数据集合

通常会绑定给能呈现集合的UI控件上,常见的就是ListBox,ListView等等列表控件了。针对于列表控件:而绑定的属

性不再是DataContext,而是ItemsSource属性,而且默认绑定模式是OneWay模式。


常见的数据绑定的数据集合:

ObservableCollection<T>集合,实现了IEnumerable接口的结合(List<T>,Collection<T>)以及自定义集合

1. ObservableCollection<T>集合

实现了INotifyCollectionChanged接口的数据集合类(注意不是INotifyPropertyChanged接口),所以它可以与列表

控件进行绑定,动态地增加集合对象或者删除数据,但是并不会将项目的属性更改通知到UI到,所以如果需要将整个

数据源绑定包括集合项的属性修改都反映到UI上,就必须给项目实现INotifyPropertyChanged接口,同之前的一篇

博客介绍的一样:http://blog.csdn.net/u010792238/article/details/45970535

2. 实现了IEnumerable接口的结合(List<T>,Collection<T>)

没有实现INotifyCollectionChanged接口的数据集合类,所以适合于绑定静态数据,也就是绑定了列表控件之后就不

需要再对列表的项目进行处插入和删除操作。

3. 自定义集合

常用的方案是IList接口,不过需要自己实现IEnumerable,INotifyCollectionChanged等接口。

IList接口是从ICollection接口派生的,ICollection是从IEnumerable接口派生的,所以IList接口同时具备了

ICollection接口和IEnumerable接口的共性。要实现IEnumerable接口(GetEnumerator),实现IList接口,实现

ICollection接口。


使用ObservableCollection集合实现动态列表:

XAML代码:

<Page
    x:Class="App1.ObservableCollectionDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <Button x:Name="add" Content="增加" Width="400" Click="add_Click"/>
            <Button x:Name="delete" Content="删除" Width="400" Click="delete_Click"/>
        </StackPanel>
        <Grid Grid.Row="1">
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="10">
                            <TextBlock Text="{Binding StuID}" FontSize="30"/>
                            <TextBlock Text="{Binding StuName}" FontSize="30"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Grid>
</Page>

.CS代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍

namespace App1
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class ObservableCollectionDemo : Page
    {

        public class StuModel
        {
            public int StuID { get; set; }
            public string StuName { get; set; }
        }

        ObservableCollection<StuModel> stuModels = new ObservableCollection<StuModel>();

        public ObservableCollectionDemo()
        {
            this.InitializeComponent();
            listView.ItemsSource = stuModels;
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            Random random = new Random();
            stuModels.Add(new StuModel() { StuID = random.Next(100), StuName = "StudentName" + random.Next(100) });
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if(listView.SelectedItem != null)
            {
                StuModel stuModel = listView.SelectedItem as StuModel;
                if (stuModels.Contains(stuModel))
                {
                    stuModels.Remove(stuModel);
                }
            }
        }
    }
}