Windows 8 series-DataTemplateSelector_IValueConverter-Essays

Source: Internet
Author: User

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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.