WPF Behavior 行为
WPF Behavior 行为
前言
行为是一类事物的共同特征,在WPF中通过行为可以封装一些通用的界面功能,从而实现代码重用来提高开发效率。因此他是一个非常好用的工具。
引入dll文件
找到System.Windows.Interactivity.dll
文件。
https://download.csdn.net/download/YouyoMei/12200463
然后将其引入到项目中。
创建行为
1.创建一个行为类LightedEffectBehavior
,继承Behavior<FrameworkElement>
,并指定行为覆盖元素类型FrameworkElement
。意思是该行为可适用于FrameworkElement
下的所有子元素。
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;namespace Deamon
{public class LightedEffectBehavior: Behavior<FrameworkElement>{}
}
2.重写Behavior
里面的两个函数OnAttached(附加后)与OnDetaching(分离时)
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;namespace Deamon
{public class LightedEffectBehavior: Behavior<FrameworkElement>{protected override void OnAttached(){base.OnAttached();// AssociatedObject 是行为的关联对象,类型为我们指定的FrameworkElementAssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;element.Effect = new DropShadowEffect() { Color = Colors.Gold, ShadowDepth = 0 };}private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;element.Effect = new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 0 };}protected override void OnDetaching(){base.OnDetaching();// 移除AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}}
}
3.通过AssociatedObject(关联对象:是行为的关联对象,类型为我们指定的FrameworkElement),实现实际行为的触发:鼠标移入,背景高亮效果。
3.1在OnAttached方法中添加鼠标响应事件处理方法。
3.2在OnDetaching方法中移除鼠标响应事件处理方法。
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;namespace Deamon
{public class LightedEffectBehavior: Behavior<FrameworkElement>{protected override void OnAttached(){base.OnAttached();// AssociatedObject 是行为的关联对象,类型为我们指定的FrameworkElementAssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e){}private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){}protected override void OnDetaching(){base.OnDetaching();// 移除AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}}
}
4.在鼠标响应事件处理方法中实现行为。
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;namespace Deamon
{public class LightedEffectBehavior: Behavior<FrameworkElement>{protected override void OnAttached(){base.OnAttached();// AssociatedObject 是行为的关联对象,类型为我们指定的FrameworkElementAssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;// 添加一个金黄色 Effect element.Effect = new DropShadowEffect() { Color = Colors.Gold, ShadowDepth = 0 };}private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;// 将 Effect 变成透明element.Effect = new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 0 };}protected override void OnDetaching(){base.OnDetaching();// 移除AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}}
}
使用行为
1.添加interactivity引用
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
2.使用行为
<Window x:Class="Deamon.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Deamon"xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel ><ListBox HorizontalAlignment="Center" Margin="20"><ListBoxItem Content="None"/><ListBoxItem Content="HasBehaviorItem"><i:Interaction.Behaviors><local:LightedEffectBehavior/></i:Interaction.Behaviors></ListBoxItem><i:Interaction.Behaviors><local:LightedEffectBehavior/></i:Interaction.Behaviors></ListBox><TextBlock Width="100" Height="30" Margin="40" Text="Hello"><i:Interaction.Behaviors><local:LightedEffectBehavior/></i:Interaction.Behaviors></TextBlock><Button Width="100" Height="30" Margin="40" Content="Deamon"><i:Interaction.Behaviors><local:LightedEffectBehavior/></i:Interaction.Behaviors></Button><CheckBox HorizontalAlignment="Center" Margin="40" Content="Melphily Deamon"><i:Interaction.Behaviors><local:LightedEffectBehavior/></i:Interaction.Behaviors></CheckBox></StackPanel></Grid>
</Window>
总结
行为与触发器有一些共同之处,很多时候可以直接使用触发器来代替,但是在做一些通用的功能时,行为不失为很好的解决方案。
Over
每次记录一小步…点点滴滴人生路…
WPF Behavior 行为相关推荐
- WPF Interaction框架简介(一)——Behavior
原文:WPF Interaction框架简介(一)--Behavior 在WPF 4.0中,引入了一个比较实用的库--Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除 ...
- WPF 行为的用法(Behavior)
WPF 行为的用法(Behavior) 行为的用法有些类似触发器的效果,但是触发器一般只能适用同一种的控件:而一个行为可以用在不同控件下(指定相同的父类): 示例: 注意先要添加引用System.Wi ...
- 【WPF】用CustomControl打造WPF版的Marquee
控件代码已经更新,支持上下左右四个方向.VS2010工程 /Files/RMay/WPF_Marquee/WpfMarquee.zip 我们知道在html中有一个marquee标签,可以很方便的实现文 ...
- wpf silverlight开发框架(prism)系列教程
弄个列表,方便你我,不要问我prism是啥,只要你做wpf or silverlight你就会知道这个东西. Prism V2之旅(1)-prism基本概览 让你了解下,prism里面的一些基本概念 ...
- WPF 基础到企业应用系列1——开篇故意
參考资料 提到參考资料,大家第一感觉就是MSDN,当然我也不例外.这个站点基本上是学习微软技术的首选站点,除了这个站点以外,我还參考了非常多其它的社区和站点,基本上都在.NET 技术社区之我见(英文篇 ...
- WPF - 本质:数据和行为
如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种不同的数据并通过UI界面展现出来,然 ...
- [Silverlight入门系列]使用MVVM模式(6):使用Behavior
Behavior把一些常用的行为封装成可重复使用的组件(Component),在理想状况下,Designer(设计师)或domain expert(特定领域的专家,例如财会人员.HR人员.或MIS)甚 ...
- WPF Datagrid with some read-only rows - Stack Overflow
原文:WPF Datagrid with some read-only rows - Stack Overflow up vote 21 down vote accepted I had the sa ...
- Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月24日-9月30日)
Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月24日-9月30日) 本周Silverlight学习资源更新 解决"Chrome提示:Silverligh ...
- WPF企业内训全程实录(中)
摘要 WPF企业内训全程实录由于文章比较长,所以一共拆分成了三篇,上篇WPF企业内训全程实录(上)主 要讲了基础,这篇作为该实录的中篇,起着承上启下的作用,主要讲解开发模式.团队协作及应用框架.其实如 ...
最新文章
- 数论基础--矩阵快速幂 及其例题
- Python基础(9)水仙花数
- 【C 语言】结构体相关 的 函数 指针 数组
- 服务器性能优化之网络性能优化
- c语言斐波那契数列_剑指Offer-10-I.斐波那契数列
- python copy与deepcopy (拷贝与深拷贝)
- app能不能跳转外部h5_利用条件编译在app端使用h5+(网页跳转 实例)
- Codeforces 1114C(数论+唯一分解)
- 离散数学第二版屈婉玲教材pdf_离散数学 第二版 [屈婉玲,耿素云,张立昂 编著] 2015年版...
- StackOverflow 推出程序员身价计算器,看看自己值多少钱?
- mouseover和mouseenter的区别?
- 域名转移应该怎么做?域名转入是什么意思?手把手教你将阿里云备案域名转入到腾讯云
- 红黑树区分 左旋 和 右旋
- android微信支付指纹支付密码错误,安卓微信支付怎么设置指纹锁
- 用户行为分析-解决某游戏公司用户数量停滞问题,给出营销策略
- Android 5.0系统特性全解析
- 【笔记整理】jq笔记
- css解决transform 方法字体抖动
- 那门用Python讲授的程序设计课程能带给学生什么
- LTE-LAA中的LBT详解
热门文章
- 用MD5验证上传文件的完整性
- 音乐指纹识别(一):音乐波形
- gpio_set_value 函数
- 双碳实力 | 谱尼成为上海市环境保护产业协会应对气候变化专委会成员单位
- 内存颗粒位宽和容量_【存储器】浅谈SDRAM内存芯片位宽
- 亮点前瞻 | 首届 ServerlesssDays · China 大会议程发布
- 战旗三国一直显示连接服务器,原来战棋三国2可以这么玩,新手玩家可别错过了!...
- 项目配置管理CM(Configuration Management)
- FFMPEG安装与视频格式flv转换mp4
- 将视频的以flv格式转换mp4格式