Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

.net - What are the different triggers in WPF?

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

I've seen the following triggers:

  • Trigger
  • DataTrigger
  • MultiTrigger
  • MultiDataTrigger
  • EventTrigger
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...