【WPF学习笔记】WPF中行为(Behavior)的简单使用
WPF中行为(Behavior)的简单使用
在我之前的 WPF 开发中,需要实现一些页面效果时,常使用到的方式就是重写元素的样式(Style),然后再样式里面设置使用元素的各类触发器(Trigger)来实现效果。这样常常需要编写大量的代码。
近日学习到 WPF 中有一个叫做 Behavior 的扩展方式,可以用来实现页面效果,实现方式更为简便。学习笔记如下:
添加需要的DLL库
要使用 Behavior 的相关功能需要先添加 “System.Windows.Interactivity” 库。可以通过 Nuget 进行安装。
如下图:
安装完成后如下图:
编写自己需要的 EffectBehavior 类
如下代码中,定义的 Behavior 类的为 EffectBehavior。EffectBehavior 需要继承自 Behavior<T>。
按 F12 进入 Behavior 的源码中可知,T (下面代码中为 FrameworkElement)为该行为需要产生作用的元素的类型,通过 AssociatedObject 进行关联。
代码如下:实现了鼠标悬停的时候,元素进行红色边框高亮显示。
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace BehaviorDemo
{
// Behavior<T> T设置行为产生作用的元素类型
public class EffectBehavior : Behavior<FrameworkElement>
{
//使用 Behavior 必须重写以下两个虚方法
protected override void OnAttached()
{
base.OnAttached();
//AssociatedObject为关联对象, 是Behavior<T>中的T
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
}
private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
var element = sender as FrameworkElement;
element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 0 };
}
private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var element = sender as FrameworkElement;
element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Red, ShadowDepth = 0 };
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
}
}
}
界面中引用 EffectBehavior
要在 XAML 代码中使用 Behavior,需要引入命名空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
具体代码如下:
<Window
x:Class="BehaviorDemo.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:BehaviorDemo"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<StackPanel>
<TextBox
Width="100"
Height="30"
Margin="40">
<i:Interaction.Behaviors>
<local:EffectBehavior />
</i:Interaction.Behaviors>
</TextBox>
<Button
Width="100"
Height="30"
Margin="40">
<i:Interaction.Behaviors>
<local:EffectBehavior />
</i:Interaction.Behaviors>
</Button>
</StackPanel>
</Grid>
</Window>
实现效果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)