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 是行为的关联对象,类型为我们指定的FrameworkElement
AssociatedObject.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 是行为的关联对象,类型为我们指定的FrameworkElement
AssociatedObject.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 是行为的关联对象,类型为我们指定的FrameworkElement
AssociatedObject.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
每次记录一小步…点点滴滴人生路…