日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF Behavior 行为

發布時間:2023/12/20 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 行为的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。