Style、ControlTemplate 和 DataTemplate 触发器
本文摘要:
1:属性触发器;
2:数据触发器;
3:事件触发器;
Style、ControlTemplate 和 DataTemplate 都有触发器集合。
属性触发器只检查WPF从属属性,而数据触发器则可检查任何一种可绑定的属性。属性触发器一般用来检查WPF可视元素的属性,而数据触发器则通常用来检查不可视对象的属性。
属性触发器:通过此机制,一个属性的更改会在另一个属性中触发即时或动态更改。
数据触发器:通过此机制,事件会在属性中触发动态更改。
数据触发器:EventTrigger,它根据事件的引发来启动一组操作,但这类操作仅限于动画。
一:属性触发器
查看代码片段1:
<Style TargetType="ListBoxItem">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="MaxHeight" Value="75" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.Setters>
<Setter Property="Opacity" Value="1.0" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
表示在ListBoxItem的IsSelected属性变为True的时候,其另外一个属性Opacity的值变为1.0。
1.1单个触发器
代码片段1就是单个触发器。
1.2多个触发器
可以为ListBoxItem设置多个触发器。
1.3多条件属性触发器
多条件触发器就是说,同时满足几个条件的时候才触发行为。
如代码片段2:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="Content" Value="{x:Null}"></Condition>
</MultiTrigger.Conditions>
<Setter Property="ToolTip" Value="content is null!"></Setter>
</MultiTrigger>
二:数据触发器
使用 DataTrigger,可以在数据对象的属性值与指定的 Value 匹配时设置属性值。例如,在显示 Employee 对象列表时,可能希望前景色根据每个 Employee 的当前出勤情况而变化。(例如,用紫色前景色显示当前正在休假的 Employee。)
查看代码片段3
<Window.Resources>
<c:Places x:Key="PlacesData"/> <Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="WA">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="Portland" />
<Condition Binding="{Binding Path=State}" Value="OR" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Cyan" />
</MultiDataTrigger>
</Style.Triggers>
</Style> <DataTemplate DataType="{x:Type c:Place}">
<Canvas Width="160" Height="20">
<TextBlock FontSize="12"
Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
<TextBlock FontSize="12" Width="30"
Canvas.Left="130" Text="{Binding Path=State}"/>
</Canvas>
</DataTemplate>
</Window.Resources> <StackPanel>
<TextBlock FontSize="18" Margin="5" FontWeight="Bold"
HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
<ListBox Width="180" HorizontalAlignment="Center" Background="Honeydew"
ItemsSource="{Binding Source={StaticResource PlacesData}}"/>
</StackPanel>
2.1单条件触发
以上的DataTrigger就是一个单条件触发器。
2.2多条件触发
以上的MultiDataTrigger就是一个多条件触发器。
三:事件触发器
属性触发器用来检查从属属性的值,数据触发器用来检查CLR属性的值,而事件触发器用来监视事件。当一个事件发生的时候,事件触发器就会通过引发相关的动画事件来响应。
如代码片段4:
<Style TargetType="ListBoxItem">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="MaxHeight" Value="75" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.Setters>
<Setter Property="Opacity" Value="1.0" />
</Trigger.Setters>
</Trigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="0:0:0.2"
Storyboard.TargetProperty="MaxHeight"
To="90" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="0:0:1"
Storyboard.TargetProperty="MaxHeight" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
Style、ControlTemplate 和 DataTemplate 触发器的更多相关文章
- ControlTemplate,ItemsPanelTemplate,DataTemplate(wpf)
在WPF中有三大模板ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和ItemsPanelTemplate是控件模板, ...
- 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
[源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...
- 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- wpf中在style的template寻找ControlTemplate和DataTemplate的控件
一.WPF中的两棵树 WPF中每个控件的Template都是由ControlTemplate构成,ControlTemplate包含了构成该控件的各种子控件,这些子控件就构成了VisualTree:而 ...
- WPF : ControlTemplate和DataTemplate的区别
ControlTemplate用于描述控件本身. 使用TemplateBinding来绑定控件自身的属性, 比如{TemplateBinding Background}DataTemplate用于描述 ...
- 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
字体的自动继承的特性 Style 样式 ControlTemplate 控件模板 示例1.演示字体的自动继承的特性Controls/UI/FontInherit.xaml <Page x:Cla ...
- WPF中ControlTemplate和DataTemplate的区别
下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...
- WPF的ControlTemplate和DataTemplate简介
首先理清几个概念,Template.ControlTemplate.ContentTemplate.DataTemplate.ContentControl 这几个东西名字都差不多,意思感觉也接近,初次 ...
随机推荐
- VS2010/MFC编程入门之四十五(MFC常用类:CFile文件操作类)
上一节中鸡啄米讲了定时器Timer的用法,本节介绍下文件操作类CFile类的使用. CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作.这些 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON Roberts2
zw版[转发·台湾nvp系列Delphi例程]HALCON Roberts2 procedure TForm1.Button1Click(Sender: TObject);var op: HOpera ...
- SoapUI 使用变量
登录问题不好解决, 只能临时用cookie来执行 1.变量定义 2.引用变量 3.调用Header
- linux常用命令:crontab 命令
前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...
- python多进程打印字符,加锁(Lock加锁)
先看不加锁的: #coding=utf-8from multiprocessing import Process,Lockimport time def l(num): #lock.acquir ...
- Linux服务器---使用mysql
使用mysql 1.登录,可以用密码登录,也可以不用密码登录.命令格式“mysql –u 用户名 –p 密码” [root@localhost src]# mysql -u root –p / ...
- 面试题之一(Spring和堆栈和逻辑运算符)
1.&和&&区别? 都是逻辑运算符,都是判断两边同时为真,否则为假:但&&当第一个为假时,后面就不执行,而&则还是要继续执行,直至结束: ——————— ...
- mpvue小程序开发入门级指南
报错指南 "Error: ERR_GET_SESSION_KEY {"code":5100,"message":"(-1)服务内部错误,请稍 ...
- Metasploit应用举例
本篇文章包含以下几方面内容: 1.Metasploit端口扫描: 2.用其他模块 3.metasploit smb获取系统信息 4.Metsploit服务识别 5.ftp识别: 6.metasploi ...
- EF、Repository、Factory、Service间关系
EF和Repository 实体(Entities):具备唯一ID,能够被持久化,具备业务逻辑,对应现实世界业务对象. 值对象(Value objects):不具有唯一ID,由对象的属性描述,一般为内 ...