zoukankan      html  css  js  c++  java
  • 使用数据模版选择器及数据触发器

    通过定义数据模板触发器,当满足某种条件时执行。如当鼠标悬停时,改变数据的背景色、前景色。
    通过定义数据模版选择器,可以根据条件将不同的模板应用于不同的数据。如本例中,年龄小于20的学生和大于20的学生使用不同的数据模板。

    先看效果:

    鼠标悬停时的效果:

    代码如下:
    <Window x:Class="DataTemplateDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:DataTemplateDemo"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:MyTemplate x:Key="template"></local:MyTemplate>
            <DataTemplate x:Key="ListBoxItem">
                <Border x:Name="brd" BorderBrush="Green" BorderThickness="2" Width="200">
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Path=First}"></Label>
                        <Label Content="{Binding Path=Last}"></Label>
                        <Label Content="{Binding Path=Age}"></Label>
                    </StackPanel>
                </Border>
                <DataTemplate.Triggers>
                    <Trigger SourceName="brd" Property="IsMouseOver" Value="True">
                        <Setter TargetName="brd" Property="Background" Value="Red"></Setter>
                        <Setter TargetName="brd" Property="BorderBrush" Value="Green"></Setter>
                        <Setter TargetName="brd" Property="BorderThickness" Value="5"></Setter>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <DataTemplate x:Key="OtherListBoxItem">
                <Border x:Name="brd" BorderBrush="Yellow" BorderThickness="2" Width="200">
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Path=First}"></Label>
                        <Label Content="{Binding Path=Last}"></Label>
                        <Label Content="{Binding Path=Age}"></Label>
                    </StackPanel>
                </Border>
                <DataTemplate.Triggers>
                    <Trigger SourceName="brd" Property="IsMouseOver" Value="True">
                        <Setter TargetName="brd" Property="Background" Value="Red"></Setter>
                        <Setter TargetName="brd" Property="BorderBrush" Value="Green"></Setter>
                        <Setter TargetName="brd" Property="BorderThickness" Value="5"></Setter>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox ItemTemplateSelector="{StaticResource template}" x:Name="lstStudent" Margin="73,47,111,72" BorderThickness="0">
                <local:Student First="aa" Last="test" Age="18"></local:Student>
                <local:Student First="bb" Last="test" Age="22"></local:Student>
                <local:Student First="cc" Last="test" Age="19"></local:Student>
                <local:Student First="dd" Last="test" Age="21"></local:Student>
            </ListBox>
        </Grid>
    </Window>
    MyTemplate类代码如下:
    public class MyTemplate:DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            DataTemplate myDataTemplate;
            FrameworkElement element = container as FrameworkElement;
            Student stuItem = item as Student;
            if (stuItem.Age < 20)
            {
                myDataTemplate = element.FindResource("ListBoxItem") as DataTemplate;
            }
            else
            {
                myDataTemplate = element.FindResource("OtherListBoxItem") as DataTemplate;
            }
            return myDataTemplate;
        }
    }
    Student类代码如下:
    namespace DataTemplateDemo
    {
        class Student
        {
            string first;

            public string First
            {
                get { return first; }
                set { first = value; }
            }
            string last;

            public string Last
            {
                get { return last; }
                set { last = value; }
            }
            int age;

            public int Age
            {
                get { return age; }
                set { age = value; }
            }
        }
    }

  • 相关阅读:
    【吐血整理】SVN命令行,Subversion的正确使用姿势,让版本控制更简单~
    【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!
    【福利大放送】不止是Android,Github超高影响力开源大放送,学习开发必备教科书
    【开发必备】今天我们来谈谈Android NDK动态链接库(so文件)的一些见解
    【使用教程】论Windows下必备的抓包工具Fiddler2如何安装证书(查看Https)
    【年终总结】我的2016,阅读本文大约需要一整年
    【知识必备】一文让你搞懂design设计的CoordinatorLayout和AppbarLayout联动,让Design设计更简单~
    【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~
    【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
    Hive 8、Hive2 beeline 和 Hive jdbc
  • 原文地址:https://www.cnblogs.com/zhouhb/p/3284928.html
Copyright © 2011-2022 走看看