博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[WPF系列]-基础系列 Property Trigger, DataTrigger & EventTrigger
阅读量:5080 次
发布时间:2019-06-12

本文共 4001 字,大约阅读时间需要 13 分钟。

So far, we worked with styles by setting a static value for a specific property. However, using triggers, you can change the value of a given property, once a certain condition changes. Triggers come in multiple flavors: Property triggers, event triggers and data triggers. They allow you to do stuff that would normally be done in code-behind completely in markup instead, which is all a part of the ongoing process of separating style and code.

Property trigger

The most common trigger is the property trigger, which in markup is simply defined with a <Trigger> element. It watches a specific property on the owner control and when that property has a value that matches the specified value, properties can change. In theory this might sound a bit complicated, but it's actually quite simple once we turn theory into an example:

   
       
           
               
           
       
   

A simple property Trigger

In this style, we set the Foreground property to blue, to make it look like a hyperlink. We then add a trigger, which listens to theIsMouseOver property - once this property changes to True, we apply two setters: We change the Foreground to red and then we make it underlined. This is a great example on how easy it is to use triggers to apply design changes, completely without any code-behind code.

We define a local style for this specific TextBlock, but as shown in the previous articles, the style could have been globally defined as well, if we wanted it to apply to all TextBlock controls in the application.

Data triggers

Data triggers, represented by the <DataTrigger> element, are used for properties that are not necessarily dependency properties. They work by creating a binding to a regular property, which is then monitored for changes. This also opens up for binding your trigger to a property on a different control. For instance, consider the following example:

   
       
       
           
               
           
       
   

A simple DataTrigger

In this example, we have a CheckBox and a TextBlock. Using a DataTrigger, we bind the TextBlock to the IsCheckedproperty of the CheckBox. We then supply a default style, where the text is "No" and the foreground color is red, and then, using a DataTrigger, we supply a style for when the IsChecked property of the CheckBox is changed to True, in which case we make it green with a text saying "Yes!" (as seen on the screenshot).

 

Event triggers

Event triggers, represented by the <EventTrigger> element, are mostly used to trigger an animation, in response to an event being called. We haven't discussed animations yet, but to demonstrate how an event trigger works, we'll use them anyway. Have a look on the chapter about animations for more details. Here's the example:

   
       
           
               
           
       
   

A simple EventTrigger

The markup might look a bit overwhelming, but if you run this sample and look at the result, you'll see that we've actually accomplished a pretty cool animation, going both ways, in ~20 lines of XAML. As you can see, I use an EventTrigger to subscribe to two events: MouseEnter and MouseLeave. When the mouse enters, I make a smooth and animated transition to a FontSize of 28 pixels in 300 milliseconds. When the mouse leaves, I change the FontSize back to 18 pixels but I do it a bit slower, just because it looks kind of cool.

Summary

WPF styles make it easy to get a consistent look, and with triggers, this look becomes dynamic. Styles are great in your application, but they're even better when used in control templates etc. You can read more about that elsewhere in this tutorial.

In the next article, we'll look at multi triggers, which allow us to apply styles based on multiple properties.

 

引用自

参考

转载于:https://www.cnblogs.com/HQFZ/p/4270296.html

你可能感兴趣的文章
java 中的多线程简单介绍
查看>>
下拉菜单被挡住了,DIV置于最底层的方法
查看>>
[转]想靠写程序赚更多钱,写到两眼通红,写得比别人都又快又好好几倍,结果又能如何?...
查看>>
验证 结构体指针与自增运算符
查看>>
实现勾选框选中之后加个勾
查看>>
VSFlexGrid控件
查看>>
Scrum Meeting---Nine(2015-11-4)
查看>>
VirtualBox使用总结
查看>>
Android 4.0 SDK官方下载
查看>>
Python—练习题
查看>>
企业——Dockerfile的编写及Dockerfile的优化
查看>>
函数调用的几种方法 几种主流框架使用匿名函数模仿块级作用域的方式
查看>>
react实现极简搜索框效果
查看>>
Spring学习笔记(一)
查看>>
IbatisNet SqlMap.config配置节导致的程序无法通过
查看>>
[java学习笔记]java语言基础概述之标识符&关键字&注释&常量和变量
查看>>
3Q大战现高潮,360 推出Android "3Q" IM即时通讯,岁末年初3Q大战惊现高潮
查看>>
分布式系统关注点(6)——「负载均衡」到底该如何实施?
查看>>
公私钥与加密、签名
查看>>
全站开发-列表讲解、切片、内置方法、元组、字典
查看>>