除了对 DataTemplateSelector和IValueConverter的基本用法外,还主要是说明了两者结合调用资源的用法。
DataTemplateSelector:cs
View Code
public class TaskListDataTemplateSelector : DataTemplateSelector { /// <summary> /// 数据模板1 /// </summary> public DataTemplate itemGridView1 { get; set; } /// <summary> /// 数据模板2 /// </summary> public DataTemplate itemGridView2 { get; set; } protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { var i = item as Item; if (i != null) { if (i.Index % 2 == 0) { return itemGridView1; } else { return itemGridView2; } } return null; } }
IValueConverter:cs
View Code
public class DateTimeConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string output = string.Empty; DateTime time; if (DateTime.TryParse(value.ToString(), out time)) { output = time.ToString("yyyy-MM-dd HH-mm"); } return output; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } }
MainPage.xaml
View Code
<Page x:Class="Win8_DataBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Win8_DataBinding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:common="using:Win8_DataBinding.Common" xmlns:convert="using:Win8_DataBinding.Convert" mc:Ignorable="d"> <Page.Resources> <!--<CollectionViewSource x:Name="cvs" IsSourceGrouped="True" ItemsPath="Items" />--> <!-- 数据源--> <CollectionViewSource x:Name="cvs1" IsSourceGrouped="true" ItemsPath="Items" /> <!-- 日前转换资源--> <convert:DateTimeConvert x:Key="DateTimeConvert" /> <!-- 数据模板1--> <DataTemplate x:Key="itemGridView1"> <StackPanel> <TextBlock Text='{Binding Title}' Foreground="Gray" FontSize="25" Margin="5" /> <TextBlock Text='{Binding Title}' Foreground="Gray" FontSize="25" Margin="5" /> </StackPanel> </DataTemplate> <!-- 数据模板2--> <DataTemplate x:Key="itemGridView2"> <StackPanel> <TextBlock Text='{Binding Title}' Foreground="Gray" FontSize="25" Margin="5" /> <TextBlock Text='{Binding DateTime, Converter={StaticResource DateTimeConvert}}' Foreground="Gray" FontSize="25" Margin="5" /> </StackPanel> </DataTemplate> <!-- 模板转换资源 分别给资源1,2赋值--> <common:TaskListDataTemplateSelector x:Key="itemTemplate" itemGridView1="{StaticResource itemGridView1}" itemGridView2="{StaticResource itemGridView2}" ></common:TaskListDataTemplateSelector> </Page.Resources> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <SemanticZoom> <SemanticZoom.ZoomedInView> <!-- 指定ItemTemplateSelector的资源--> <GridView x:Name="gridView" ItemsSource="{Binding Source={StaticResource cvs1}}" SelectionMode="None" ItemTemplateSelector="{StaticResource itemTemplate}"> <GridView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock Text='{Binding Key}' Foreground="Gray" FontSize="25" Margin="5" /> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </GridView.GroupStyle> </GridView> </SemanticZoom.ZoomedInView> <SemanticZoom.ZoomedOutView> <GridView x:Name="outView" > <GridView.ItemTemplate> <DataTemplate> <TextBlock Text='{Binding Group.Key}' Foreground="Gray" FontSize="25" Margin="5" /> </DataTemplate> </GridView.ItemTemplate> </GridView> </SemanticZoom.ZoomedOutView> </SemanticZoom> </Grid> </Page>