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企业内训全程实录(上)主 要讲了基础,这篇作为该实录的中篇,起着承上启下的作用,主要讲解开发模式.团队协作及应用框架.其实如 ...
最新文章
- 利用OpenGL渲染并用OpenCV输出显示二维图像
- 身份证被盗用申请信用卡,造成逾期被催收怎么办?
- Maven集成测试和Spring Restful Services
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
- 李洋疯狂C语言之选择排序
- MySQL 练习 创建表格2
- JavaWeb知识总结
- [转载] numpy的属性和函数
- linux——网络情报收集(总结篇.干货+实战)
- 华为android已锁定,教你如何查看华为手机是否己解锁bootloader
- IDEA热部署之JRebel破解附反向代理破解exe(安装)
- 嵌入式系统多媒体框架分析
- ipadpro尺寸的html,iPad Pro尺寸到底有多大?
- Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day33】—— 手撸算法2
- 图片标注软件labelImg使用指南
- 蓝桥 卷“兔”来袭编程竞赛专场-10仿射加密 题解
- 2D 横向对抗射击游戏之美
- 牛客每日练习----骑行川藏,Lucky Coins,不凡的夫夫
- android+8g开机剩余,8G内存的iQOO开机只剩4G,Jovi没用,网友直言后悔了
- L1-020 帅到没朋友
热门文章
- 用计算机算出女儿身高,孩子身高预测计算器准吗
- wps打开pdf乱码_用wps2016版打开word文件乱码怎么处理
- cocos2d - JS Sprite 镜像反转显示 ( Flipped )
- flv怎么转换成html5,快速教你如何将FLV转换MP4格式
- 关于解决Visual Studio 实时调试器弹出的对话框问题 三种解决办法
- 光合作用c3和c5变化语言叙述,浅议光合作用中C3和C5的含量变化
- 即时通讯(IM)开源项目OpenIM本周版本发布-v1.0.6
- 爆破字典生成器kali之crunch
- 集成灶哪个品牌性价比高质量好,过来人实话实说?
- iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5000 -j DNAT --to-destinatio