Windows 8 系列 — DataTemplateSelector_IValueConverter –隨筆

來源:互聯網
上載者:User

除了對 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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.