什么是WPF中不同的触发器?触发器、不同、WPF

2023-09-03 07:42:43 作者:ㄒ壹站→幸福

什么是WPF中不同的触发器?它们有什么不同,当我要使用它们?

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

我看到以下触发器:

在触发 DataTrigger MultiTrigger MultiDataTrigger 的EventTrigger

推荐答案

一个触发器通常用于样式或控件模板。它触发对被模板化的东西属性,并设置控制(或特定的模板元素)的其它性质。例如,你可以使用一个触发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。例如,你可以使用一个DataTrigger更改元素的颜色在DataTemplate中,如果AlertLevel属性等于ZomgWereAllGoingToDie。如果你想在转变控件属性触发(即使用在触发测试的的IValueConverter)DataTriggers也可以在控件模板非常有用。例如,你可以使用一个DataTrigger把一个文本框的前景红色的,如果Text属性,作为一个数字,结果为阴性,用DataTrigger用合适的IValueConverter和自我或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.