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. MHA 的 Binlog Server & VIP 漂移

    目录 Binlog Server 在 MHA 配置文件中配置 Binlog Server 创建 Binlog 存放目录 实时传输主库 Binlog 命令 重启 MHA 检验 MHA Manager 服 ...

  2. leetcode 22. 括号生成 dfs

    先思考符合要求的串是什么样子的 任意时刻,(数量大于),且最后(==)==n即可 考虑下一个加入string的字符时(或者)即可 dfs class Solution { public: vector ...

  3. codeforces 1059C. Sequence Transformation【构造】

    题目:戳这里 题意:有1,2,3...n这n个数,求一次这些数的gcd,删去一个数,直到剩下一个数为止.输出这n个gcd的最大字典序. 解题思路:一开始的gcd肯定是1,要让字典序最大,我们可以想到下 ...

  4. ewebeditor 路径

    1.关键文件的名称和路径Admin_Login.asp 登录页面Admin_Default.asp 管理首页Admin_Style.aspAdmin_UploadFile.aspUpload.aspA ...

  5. 输入函数input()、运算符

    一.input()函数的基本使用 present = input('大圣想要什么礼物') 作用:接受来自用户的输入 返回值类型:输入值的类型为str 值的存储:使用 = 对输入的值进行存储 name= ...

  6. swiper & swiper slider

    swiper & swiper slider mobile swiper https://idangero.us/swiper/ https://idangero.us/swiper/get- ...

  7. How to enable HTTPS for local development in macOS using Chrome

    How to enable HTTPS for local development in macOS using Chrome HTTPS, macOS, Chrome local HTTPS htt ...

  8. HOC in Depth

    HOC in Depth Higher-Order Components https://reactjs.org/docs/higher-order-components.html 1. wrappe ...

  9. Flutter: 获取本地json数据

    FutureBuilder( future: DefaultAssetBundle.of(context).loadString('data/data.json'), builder: (contex ...

  10. SPC空投火爆来袭!区块链技术落地加速!

    经历市场狂热后,区块链逐渐恢复合理性,在政策红利.技术等多力推进下,各行各业开始涌入区块链行业.在这波浪潮中,SPC侧链代币项目显得格外亮眼,其空投已经发放至第二轮,仅SPC空投月收益就达23%左右, ...