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>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的设计特性。使用行为的地方,也是可以使用触发器取代的。不过行为使用起来也是有趣的.在做一些通用的功能时,行为不失为很好的解决方案。