1. Trigger

1.1 由属性值触发的 Trigger

最基本的触发器,Property 是关注的属性名称,value 是触发条件,一旦触发条件满足,就会应用 Trigger 的 Setters,触发条件不再满足时,各属性值会被还原

<StackPanel>
<StackPanel.Resources>
<Style TargetType="CheckBox" >
<Setter Property="Height" Value="25"/>
<Setter Property="Margin" Value="5 0"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="FontSize" Value="15"/>
<Setter Property="Foreground" Value="LightSkyBlue"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<CheckBox Content="篮球" />
<CheckBox Content="香蕉" />
<CheckBox Content="单车" />
<CheckBox Content="自驾" />
</StackPanel>

1.2 MultiTrigger

MultiTrigger 指多个条件下同时成立时触发,MultiTrigger 比 Trigger 多了一个 Conditions 属性,需要同时成立的条件就在这个集合里。

<StackPanel>
<StackPanel.Resources>
<Style TargetType="CheckBox" >
<Setter Property="Height" Value="25"/>
<Setter Property="Margin" Value="5 0"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="FontSize" Value="15"/>
<Setter Property="Foreground" Value="LightSkyBlue"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True"/>
<Condition Property="Content" Value="香蕉"/>
</MultiTrigger.Conditions>
<Setter Property="FontSize" Value="17"/>
<Setter Property="Foreground" Value="Red"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources> <CheckBox Content="篮球" />
<CheckBox Content="香蕉" />
<CheckBox Content="单车" />
<CheckBox Content="自驾" />
</StackPanel>

多个 Trigger 或 MultiTrigger 时,会按代码的顺序触发,比如点击香蕉,FontSize 最终是 17,如果把 MultiTrigger 实例写在 Trigger 实例前面,点击香蕉后,FontSize 的值是 Trigger 的 15 。

1.3 由数据触发的 DataTrigger

<StackPanel>
<local:LongToBoolConverter x:Key="cvtltb"/> <StackPanel.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self},
Path=Text.Length,
Converter={StaticResource cvtltb}}"
Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBox />
</StackPanel> public class LongToBoolConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value > 6 ? true : false;
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

1.4 MultiDataTrigger

public class Student
{
public string Id { get; set; } public string Name { get; set; } public bool LoveBasketball { get; set; }
} ... <Window.Resources>
<coll:ArrayList x:Key="stuList">
<local:Student Id="1" Name="D1" LoveBasketball="True"></local:Student>
<local:Student Id="2" Name="D2" LoveBasketball="False"></local:Student>
<local:Student Id="3" Name="D3" LoveBasketball="True"></local:Student>
<local:Student Id="3" Name="D4" LoveBasketball="False"></local:Student>
<local:Student Id="5" Name="D5" LoveBasketball="True"></local:Student>
<local:Student Id="6" Name="D6" LoveBasketball="False"></local:Student>
</coll:ArrayList>
</Window.Resources> <StackPanel MenuItem.Click="StackPanel_Click">
<StackPanel.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate" Value="{StaticResource it}"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Id}" Value="3"/>
<Condition Binding="{Binding Name}" Value="D3"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red"></Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources> <ListBox ItemsSource="{StaticResource stuList}"/>
</StackPanel>

1.5 由事件触发的 EventTrigger

EventTrigger 触发后并非应用一组 Setter,而是执行一段动画,因此 UI 的动画效果往往跟 EventTrigger 相关联。

<Button Content="确认">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="30"/>
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="400" Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
<DoubleAnimation To="60" Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

微软官方文档中对 Trigger 的介绍很简洁明了:

https://docs.microsoft.com/zh-cn/dotnet/desktop/wpf/fundamentals/styles-templates-overview?view=netdesktop-5.0#triggers

WPF 基础 - Trigger的更多相关文章

  1. WPF基础到企业应用系列6——布局全接触

    本文转自:http://knightswarrior.blog.51cto.com/1792698/365351 一. 摘要 首先很高兴这个系列能得到大家的关注和支持,这段时间一直在研究Windows ...

  2. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  3. WPF笔记(1.1 WPF基础)——Hello,WPF!

    原文:WPF笔记(1.1 WPF基础)--Hello,WPF! Example 1-1. Minimal C# WPF application// MyApp.csusing System;using ...

  4. WPF 杂谈——Trigger触发器

    笔者在使用的WPF过程中,见过的触发器有三种:Trigger.DataTrigger.EventTrigger.其中最为常用的要属Trigger.至于触发器的作用就是当某个属性的值发生变化,应该去做某 ...

  5. 如何在 UWP 使用 wpf 的 Trigger

    本文需要告诉大家,如何使用 Behaviors 做出 WPF 的 Trigger ,需要知道 UWP 不支持 WPF 的 Trigger . 安装 Behaviors 请使用 Nuget 安装,可以输 ...

  6. WPF触发器(Trigger)

    WPF触发器(Trigger.DataTrigger.EventTrigger) WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改 ...

  7. C# WPF基础巩固

    时间如流水,只能流去不流回. 学历代表你的过去,能力代表你的现在,学习能力代表你的将来. 学无止境,精益求精. 一.写作目的 做C# WPF开发,无论是工作中即将使用,还是只应付跳槽面试,开发基础是非 ...

  8. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)

    一. 摘要 首先圣殿骑士非常高兴这个系列能得到大家的关注和支持.这个系列从七月份開始到如今才第七篇,上一篇公布是在8月2日,掐指一算有二十多天没有继续更新了,最主要原因一来是想把它写好,二来是由于近期 ...

  9. WPF基础知识、界面布局及控件Binding(转)

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

随机推荐

  1. Python进阶丨如何创建你的第一个Python元类?

    摘要:通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类. Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一. ...

  2. 要想用活Redis,Lua脚本是绕不过去的坎

    前言 Redis 当中提供了许多重要的高级特性,比如发布与订阅,Lua 脚本等.Redis 当中也提供了自增的原子命令,但是假如我们需要同时执行好几个命令的同时又想让这些命令保持原子性,该怎么办呢?这 ...

  3. Go中的Socket编程

    在很多底层网络应用开发者的眼里一切编程都是Socket,话虽然有点夸张,但却也几乎如此了,现在的网络编程几乎都是用Socket来编程.你想过这些情景么?我们每天打开浏览器浏览网页时,浏览器进程怎么和W ...

  4. Git使用指南(下)

    9 初识分支 把每一次的提交,都用线连起来,你会发现,很连贯. C/C++    指针的概念 git reset --hard commitid HEAD    如果说内容已经add到暂存区,此时要想 ...

  5. 自己yy的中缀表达式转后缀表达式(未验证完全正确)

    目前自己测试的表达式都没有出过问题 思路是这样,先将后缀表达式的计算顺序搞出来..当完全缩出来一个数的时候,如果后面还有要计算的,我们就把它放到后缀表达式的后面 先算后面的..不断迭代.. #incl ...

  6. 解决debian (Friendly ARM 嵌入式板)的sudo等一部分命令无法TAB补全

    TAB对于比较长的命令在使用时是十分方便的,最近就遇到TAB 键无法补全sudo后跟的命令的情况因此去网上取经.在一篇博客中找到解决问题的方法,觉得大牛们写的太精炼然后自己做如下总结方便自已以后解决类 ...

  7. SVG in Action

    SVG in Action HTML5 semantic HTML5 Semantic Elements / HTML5 Semantic Tags figure object <figure& ...

  8. CSS Learning Paths

    CSS Learning Paths CSS Expert refs https://developer.mozilla.org/en-US/docs/Web/CSS https://css-tric ...

  9. Flutter for web

    Flutter for web https://flutter.dev/web https://github.com/flutter/flutter_web Dart https://github.c ...

  10. SVG & gradient & color

    SVG & gradient & color https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Gradients & ...