In addition to the basic usage of DataTemplateSelector and IValueConverter, it mainly describes the usage of the two in combination to call resources.
DataTemplateSelector: cs
View Code
Public class TaskListDataTemplateSelector: DataTemplateSelector {// <summary> // data template 1 /// </summary> public DataTemplate itemGridView1 {get; set ;} /// <summary> // data template 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: Win 8_DataBinding.Convert "mc: Ignorable =" d "> <Page. Resources> <! -- <CollectionViewSource x: Name = "cvs" IsSourceGrouped = "True" ItemsPath = "Items"/> --> <! -- Data source --> <CollectionViewSource x: Name = "cvs1" IsSourceGrouped = "true" ItemsPath = "Items"/> <! -- Convert resources by day --> <convert: DateTimeConvert x: Key = "DateTimeConvert"/> <! -- Data Template 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> <! -- Data Template 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> <! -- Template conversion resources assign values to resource 1 and 2 respectively --> <common: TaskListDataTemplateSelector x: key = "itemTemplate" itemGridView1 = "{StaticResource itemGridView1}" itemGridView2 = "{StaticResource itemGridView2}"> </common: TaskListDataTemplateSelector> </Page. resources> <Grid Background = "{StaticResource ApplicationPageBackgroundThemeBrush}"> <SemanticZoom. zoomedInView> <! -- Specify the resource of ItemTemplateSelector --> <GridView x: name = "gridView" ItemsSource = "{Binding Source = {StaticResource cvs1}" SelectionMode = "None" ItemTemplateSelector = "{StaticResource itemTemplate}"> <GridView. 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>