WPF 行为

WPF 行为

一、前言

行为是一类事物的共同特征,可以向用户界面控件添加功能,而无需将其子类化。 功能是在行为类中实现的,并附加到控件上,就像它本身就是控件的一部分。

比如在鼠标进入/离开控件时,表现出不同的现象;给TextBox添加水印等

二,实例

我们现在创建一个Button行为,当鼠标进入时,将按键放大并添加与阴影,以实现浮动效果,离开时,复原

1. 创建一个类继承自Behavior

csharp 复制代码
public class MyButtonBehavior : Behavior<Button>

我们自定义的行为需要继承自Behavior,并且需要指定该行为所用于的控件类型 ,该例子指定的关联对象类型为Button

2. 重写方法OnAttached()

在该方法中获取Button的宽和高,保存到字段中,并订阅Button的鼠标进入(MouseEnter)和鼠标离开事件(MouseLeave) AssociatedObject在这里就是所附加该行为的对象

在使用的过程中需要注意两点

  • 在自己实现的行为中,需要重写两个虚方法OnAttached() 和OnDetaching(),在行为附加和分离时会分别调用这两个方法
  • 在Behavior中有一个AssociatedObject的属性,该属性就是关联对象,也就是附加在控件对象
csharp 复制代码
public class MyButtonBehavior : Behavior<Button>
{

    private double m_Width;
    private double m_Hight;
    protected override void OnAttached()
    {
        base.OnAttached();

        //回去button的宽和高
        m_Width = AssociatedObject.Width;
        m_Hight = AssociatedObject.Height;

        AssociatedObject.MouseEnter += AssociatedObject_MouseEnter; 
        AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
        AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
    }

    private void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
    {
        Button btn = sender as Button;

        Storyboard storyboard = new Storyboard();

        DoubleAnimation daw = new DoubleAnimation();
        daw.Duration = TimeSpan.FromMilliseconds(100);
        daw.To = m_Width;

        DoubleAnimation dah = new DoubleAnimation();
        dah.Duration = TimeSpan.FromMilliseconds(100);
        dah.To = m_Hight;

        Storyboard.SetTarget(daw, btn);
        Storyboard.SetTargetProperty(daw, new PropertyPath("Width"));
        Storyboard.SetTarget(dah, btn);
        Storyboard.SetTargetProperty(dah, new PropertyPath("Height"));

        storyboard.Children.Add(daw);
        storyboard.Children.Add(dah);
        storyboard.Begin();

        btn.Effect = null;
    }

    private void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
    {
        Button btn = sender as Button;

        //设置动画
        Storyboard storyboard = new Storyboard();

        DoubleAnimation daw = new DoubleAnimation();
        daw.Duration = TimeSpan.FromMilliseconds(100);
        daw.To = m_Width * 1.2;

        DoubleAnimation dah = new DoubleAnimation();
        dah.Duration = TimeSpan.FromMilliseconds(100);
        dah.To = m_Hight *  1.2;

        Storyboard.SetTarget(daw, btn);
        Storyboard.SetTargetProperty(daw, new PropertyPath("Width"));
        Storyboard.SetTarget(dah, btn);
        Storyboard.SetTargetProperty(dah, new PropertyPath("Height"));

        storyboard.Children.Add(daw);
        storyboard.Children.Add(dah);
        storyboard.Begin();

        //设置阴影
        DropShadowEffect dropShadowEffect = new DropShadowEffect();
        dropShadowEffect.ShadowDepth = 1;
        dropShadowEffect.BlurRadius = 10;
        dropShadowEffect.Color = Color.FromRgb(221, 221, 221);
        btn.Effect = dropShadowEffect;
    }
}

安装NuGet包,Microsoft.Xmal.Behaviors.Wpf, 并在xaml界面引用命名空间 xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

csharp 复制代码
<Button Content="Clear"
        Margin="10 0 0 0"
        Width="100"
        Height="30">
    <i:Interaction.Behaviors>
        <local:MyButtonBehavior/>
    </i:Interaction.Behaviors>
</Button>

这样就实现了鼠标进入放大,鼠标离开恢复原样的效果

相关推荐
芝麻科技1 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆2 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退2 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu2 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退5 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆6 天前
WPF中的XAML详解
wpf·xaml
ithouse6 天前
使用WPF实现一个快速切换JDK版本的客户端工具
java·开发语言·wpf
河西石头6 天前
WPF之UI进阶--控件样式与样式模板及词典
ui·wpf·样式·模板·控件样式·样式模板·样式词典
TA远方7 天前
【WPF】桌面程序开发之窗口的用户控件详解
c#·wpf·usercontrol·用户控件·控件属性