• 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交流
  • 相关阅读:
    如何自定义长连接策略
    知物由学 | 这些企业大佬如何看待2018年的安全形势?
    网易云易盾朱星星:最容易被驳回的10大APP过检项
    网易云易盾朱浩齐:视听行业步入强监管和智能时代
    知物由学 | 人工智能、机器学习和深度学习如何在网络安全领域中应用?
    知物由学 | 广告欺诈:如何应对数字广告里分羹者?
    知物由学 | 如何应对日益强大的零日攻击
    不再任人欺负!手游安全的进阶之路
    邪恶的三位一体:机器学习、黑暗网络和网络犯罪
    PAT 1053. Path of Equal Weight
  • 原文地址:https://www.cnblogs.com/Jusoc/p/2770655.html
Copyright © 2020-2023  润新知