WPF Behavior 行为

    技术2022-07-10  155

    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>

    例子:Image 添加Command和CommandParameter

    public class ImageClick { public static DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ImageClick), new UIPropertyMetadata(CommandChanged)); public static DependencyProperty CommandParameterproperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ImageClick), new UIPropertyMetadata(null)); public static void SetCommand(DependencyObject target,ICommand value) { target.SetValue(CommandProperty,value); } public static void SetCommandParameter(DependencyObject target, ICommand value) { target.SetValue(CommandParameterproperty, value); } public static object GetCommandParameter(DependencyObject target) { return target.GetValue(CommandParameterproperty); } private static void CommandChanged(DependencyObject target,DependencyPropertyChangedEventArgs e) { var control = target as System.Windows.Controls.Image; if(control!=null) { if((e.NewValue!=null)&&(e.OldValue==null)) { control.MouseLeftButtonDown += OnMouseDoubleClick; } else if((e.NewValue==null)&&(e.OldValue!=null)) { control.MouseLeftButtonDown -= OnMouseDoubleClick; } } } private static void OnMouseDoubleClick(object sender,RoutedEventArgs e) { var control =sender as System.Windows.Controls.Image; ; ICommand command = (ICommand)control.GetValue(CommandProperty); object commandParameter = control.GetValue(CommandParameterproperty); command.Execute(commandParameter); } }

    使用

    1.添加引用

    xmlns:behaviors="clr-namespace:yang.behaviors;assembly=yang.behaviors"

     

    2.使用

    <Image Source="/yang;component/Images/go.png" Grid.column="3" behaviors:ImageClick.Command="{binding OnSearch}" behaiors:ImageClick.CommandParameter="{Binding SelectedItems,ElementName=uxFilter}"/>

    总结

    行为并不是WPF中的核心的部分,是Expression Blend的设计特性。使用行为的地方,也是可以使用触发器取代的。不过行为使用起来也是有趣的.在做一些通用的功能时,行为不失为很好的解决方案。

    Processed: 0.010, SQL: 9