Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(53) 绑定

Windows 8 Store Apps学习(53) 绑定2014-03-09 cnblogs webabcd介绍

重新想象 Windows 8 Store Apps 之 绑定

与 ObservableCollection 绑 定

与 CollectionViewSource 绑定

与 VirtualizedFilesVector 绑定

对 VirtualizedItemsVector 绑定

示例

1、演示如何绑定 ObservableCollection<T> 类型的数据

Binding/BindingObservableCollection.xaml

<Pagex:Class="XamlDemo.Binding.BindingObservableCollection"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.Binding"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><Grid Margin="120 0 0 10"><Grid.Resources><DataTemplate x:Key="MyDataTemplate"><Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left"><TextBlock Text="{Binding Name}" FontSize="14.667" /></Border></DataTemplate></Grid.Resources><StackPanel Orientation="Horizontal" VerticalAlignment="Top"><Button Name="btnDelete" Content="删除一条记录" Click="btnDelete_Click_1" /><Button Name="btnUpdate" Content="更新一条记录" Click="btnUpdate_Click_1" Margin="10 0 0 0" /></StackPanel><ListView x:Name="listView" ItemTemplate="{StaticResource MyDataTemplate}" Margin="0 50 0 0" /></Grid></Grid></Page>
Binding/BindingObservableCollection.xaml.cs

/* * 演示如何绑定 ObservableCollection<T> 类型的数据 ** ObservableCollection<T> - 在数据集合进行添加项、删除项、更新项、移动项等操作时提供通知 * CollectionChanged - 当发生添加项、删除项、更新项、移动项等操作时所触发的事件(事件参数:NotifyCollectionChangedEventArgs) */using System;using System.Collections.ObjectModel;using System.Collections.Specialized;using System.Linq;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using XamlDemo.Model;namespace XamlDemo.Binding{public sealed partial class BindingObservableCollection : Page{private ObservableCollection<Employee> _employees;public BindingObservableCollection(){this.InitializeComponent();this.Loaded += BindingObservableCollection_Loaded;}void BindingObservableCollection_Loaded(object sender, RoutedEventArgs e){_employees = new ObservableCollection<Employee>(TestData.GetEmployees());_employees.CollectionChanged += _employees_CollectionChanged;listView.ItemsSource = _employees;}void _employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e){/* * e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举) * Add, Remove, Replace, Move, Reset * e.OldItems - Remove, Replace, Move 操作时影响的数据列表 * e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引 * e.NewItems - 更改中所涉及的新的数据列表 * e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引 */}private void btnDelete_Click_1(object sender, RoutedEventArgs e){_employees.RemoveAt(0);}private void btnUpdate_Click_1(object sender, RoutedEventArgs e){Random random = new Random();// 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee_employees.First().Name = random.Next(1000, 10000).ToString(); // 此处的通知来自 ObservableCollection<T>_employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };}}}