WPF:属性触发器
来源:互联网 发布:java中级招聘 编辑:程序博客网 时间:2024/05/22 10:48
无论何时,只要依赖属性的值改变了,WPF就会自动根据属性的元数据(metadata)触发一系列动作。这些动作可以重新呈现适当的元素、更新当前布局、刷新数据绑定等。内建的变更通知最有趣的特性之一是属性触发器,它可以在属性值改变时执行自定义动作,而不用更改任何过程式代码。
例:为一个按钮设置属性:在鼠标指针移上去时按钮上的字变为蓝色。
如果没有属性触发器的话,你得为每个Button添加两个事件处理程序,一个是为MouseEvent事件准备的,一个是为MouseLeave事件准备的。
<Button MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" MinWidth="75" Margin="10">Help</Button>在相应的隐藏代码中添加如下事件处理:// 当鼠标进入按钮区域时,改变foreground为蓝色 void Button_MouseEnter(object sender, MouseEventArgs e) { Button b = sender as Button; if (b != null) { b.Foreground = Brushes.Blue; } } // 当鼠标离开按钮区域时,恢复foreground为黑色 void Button_MouseLeave(object sender, MouseEventArgs e) { Button b = sender as Button; if (b != null) { b.Foreground = Brushes.Black; } }然而,有了属性触发器,你可以完全在XAML中完成相同的行为。下面Trigger对象就是需要写的所有代码:(红色部位为将该该属性触发器通过style对象应用到Button上去)
<Button MinWidth="75" Margin="10"> Help <Button.Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Blue"/> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button>这个触发器能够基于Button的IsMouseOver属性工作,当MouseEnter触发时,IsMouseOver属性会变为true;在MouseLeave触发时,它又变为false。注意,当IsMouseOver变为false时,不用把Foregound变为黑色,这是WPF自动完成的!
- WPF:属性触发器
- WPF:属性触发器
- WPF 自定义属性和触发器
- WPF 属性和触发器 简单示例 备忘
- wpf-触发器
- WPF 触发器
- WPF触发器
- WPF触发器
- WPF触发器
- WPF 触发器
- WPF触发器
- WPF-触发器
- WPF 触发器
- WPF-11:WPF触发器
- WPF – 使用触发器
- WPF – 使用触发器
- WPF 多种触发器代码
- WPF 样式触发器
- C++开发python扩展模块:ImportError: dynamic module does not define init function (initRabbit)解决办法
- string 最佳实践(C#版) (IS2120@CSDN)
- Flex文本框自动提示(AutoSuggest)、自动完成(AutoComplete)
- 毕业设计管理系统小结
- 在用scott用户导出dmp文件后,用sys用户执行IMP语句出现ORA-28009 connection to sys should be as sysdba or sysoper错误
- WPF:属性触发器
- 函数返回值类型问题
- oracle使用小结
- 初识svn
- IC前端设计絮论
- Android使用命令行打APK包
- ArcMap编辑SQLServer的ArcSDE图层提示:The Create Feature task could not be completed.
- 利用OpenCV求取圖像的重心
- 凸包求法