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 行为的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 金色css颜色代码大全,CSS颜色代码大
- 下一篇: hr有必要学python吗_人力资源分析