天天看點

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

繼續閱讀