zoukankan      html  css  js  c++  java
  • Windows 8 系列 DataTemplateSelector_IValueConverter 随笔

    除了对 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>
    学徒帮-jQuery帮帮帮 欢迎更多的前端交流、Js交流、jQuery交流
  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/Jusoc/p/2770655.html
Copyright © 2011-2022 走看看