WPF 中有哪些不同的触发器? [英] What are the different triggers in WPF?

查看:29
本文介绍了WPF 中有哪些不同的触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 中有哪些不同的触发器?它们有何不同,我应该何时使用它们?

What are the different triggers in WPF? How do they differ and when should I use them?

我见过以下触发器:

  • 触发
  • 数据触发器
  • 多重触发
  • 多数据触发器
  • 事件触发器

推荐答案

触发器通常用于 Style 或 ControlTemplate.它触发正在模板化的事物的属性,并设置控件(或特定模板元素)的其他属性.例如,您可以在 IsMouseOver 上使用触发器来响应鼠标悬停在控件上,并且设置器可能会更新画笔以显示热"效果.

A Trigger is typically used in a Style or ControlTemplate. It triggers on properties of the thing being templated, and sets other properties of the control (or of specific template elements). For example, you would use a Trigger on IsMouseOver to respond to the mouse being over the control, and the setters might update a brush to show a "hot" effect.

DataTrigger 在数据绑定而不是控件属性上触发.它通常用于 DataTemplate.例如,如果 AlertLevel 属性等于 ZomgWereAllGoingToDie,您可以使用 DataTrigger 更改 DataTemplate 中元素的颜色.如果您想在转换后的"控件属性上触发(即在触发器测试中使用 IValueConverter),则 DataTriggers 在控件模板中也很有用.例如,如果 Text 属性(被视为数字)为负数,您可以使用 DataTrigger 将 TextBox 的 Foreground 变为红色,方法是使用带有合适的 IValueConverter 的 DataTrigger 和 Self 或 TemplatedParent 的 RelativeSource.

A DataTrigger triggers on a data binding rather than on a control property. It is typically used in a DataTemplate. For example, you could use a DataTrigger to change the colour of an element in the DataTemplate if the AlertLevel property was equal to ZomgWereAllGoingToDie. DataTriggers can also be useful in control templates if you want to trigger on a "converted" control property (i.e. use an IValueConverter in the trigger test). For example, you could use a DataTrigger to turn a TextBox's Foreground red if the Text property, considered as a number, was negative, by using a DataTrigger with a suitable IValueConverter and a RelativeSource of Self or TemplatedParent.

MultiTrigger 和 MultiDataTrigger 是一样的,只是它们允许你指定多个条件(分别是属性或绑定),并且只有在所有条件都满足时才生效.

MultiTrigger and MultiDataTrigger are the same, except they allow you to specify multiple conditions (properties or bindings respectively) and take effect only when all conditions are satisfied.

最后,EventTrigger 用于触发动作以响应事件(而不是更改一个状态以响应另一个状态).例如,您可以使用 EventTrigger 来响应 MouseEnter 事件.EventTriggers 通常用于执行故事板,例如在事件发生时执行动画.

Finally, EventTrigger is used to trigger actions in response to events (as opposed to changing one piece of state in response to another piece of state). For example, you could use an EventTrigger to respond to the MouseEnter event. EventTriggers are typically used to execute storyboards, for example to perform an animation when the event occurs.

这篇关于WPF 中有哪些不同的触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆