ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【WPF】Behavior 行为

2022-08-15 02:01:11  阅读:191  来源: 互联网

标签:MouseLeave Windows AssociatedObject System Behavior using WPF 行为 MouseEnter


前言


行为是一类事物的共同特征,在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>

总结

行为与触发器有一些共同之处,很多时候可以直接使用触发器来代替,但是在做一些通用的功能时,行为不失为很好的解决方案。

 

标签:MouseLeave,Windows,AssociatedObject,System,Behavior,using,WPF,行为,MouseEnter
来源: https://www.cnblogs.com/cdaniu/p/16586877.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有