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
每次記錄一小步…點點滴滴人生路…