wpf之行为

前言

行为是WPF中用于增强UI元素功能的一种重要模式,它允许在不修改原始控件代码的情况下,为控件添加交互逻辑。它可以封装某些功能(如拖放、命令执行、状态管理等),使这些功能可以在不同控件间复用

1、新建行为类

下面的代码给所有控件添加一个行为,添加行为分为两个步骤,第一步就是新建一个类继承Behavior,第二步就是在view视图代码中给指定的控件附加行为, Behavior中的T用于指定行为可以用在哪种控件上,这里使用了Control,由于Control是所有控件的基类,所以所以这个行为可以被所有控件使用,当鼠标进入控件时(在控件上方)更改控件的字体颜色为黄色,当鼠标离开控件时恢复控件原来的字体颜色。行为主要是重写OnAttached和OnDetaching方法,在OnAttached方法中订阅事件,在OnDetaching方法方法中取消订阅事件。下面的代码中在OnAttached订阅了MouseEnter和MouseLeave两个事件。

csharp 复制代码
class ChangeForegroundBehavior : Behavior<Control>
    {
        private Brush originalForeground;
        private static readonly Brush hoverBrush = Brushes.Yellow  ;

        protected override void OnAttached()
        {
            base.OnAttached();
            originalForeground = AssociatedObject.Foreground ;
            AssociatedObject.MouseEnter += OnMouseEnter;
            AssociatedObject.MouseLeave += OnMouseLeave;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            // 取消订阅事件
            AssociatedObject.MouseEnter -= OnMouseEnter;
            AssociatedObject.MouseLeave -= OnMouseLeave;
        }

        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            // 当鼠标进入时,改变字体颜色
            AssociatedObject.Foreground  = hoverBrush;
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            // 当鼠标离开时,恢复字体颜色
            AssociatedObject.Foreground = originalForeground;
        }
    }

2、给指定控件添加行为

下面的代码中分别给Label 和Button添加ChangeForegroundBehavior行为,

csharp 复制代码
<Window x:Class="行为.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:行为"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:ei="clr-namespace:Microsoft.Xaml.Behaviors.Core;assembly=Microsoft.Xaml.Behaviors"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions >
        <Label   Grid.Row=" 0" Content="test" Width="100" Height="30" Foreground="Red"  Background="Blue"  FontSize="15">
            <i:Interaction.Behaviors>
                <local:ChangeForegroundBehavior />
            </i:Interaction.Behaviors>
        </Label>
        <Button  Grid.Row=" 1" Content="test" Width="100" Height="30" Foreground="Red"  FontSize="15">
            <i:Interaction.Behaviors>
                <local:ChangeForegroundBehavior />
            </i:Interaction.Behaviors>
        </Button>
    </Grid>
</Window>

1)鼠标放在Label上方时

2)鼠标离开Label上方时

相关推荐
游乐码19 小时前
c#万物之父装箱拆箱
开发语言·c#
GIS程序猿19 小时前
批量出图工具,如何使用C#实现动态文本
开发语言·arcgis·c#·arcgis插件·gis二次开发
量子物理学19 小时前
三、C#高级进阶语法——特性(Attribute)
java·算法·c#
量子物理学20 小时前
四、C#高级进阶语法——委托(Delegate)
开发语言·c#
bepeater123421 小时前
Laravel9.X核心特性全面解析
c语言·c++·c#·php
lpfasd1231 天前
Markdown 导出 Word 文档技术方案
开发语言·c#·word
m5655bj1 天前
通过 C# 将 PPT 文档转换为 HTML 格式
c#·html·powerpoint
未来之窗软件服务1 天前
AI人工智能(十五)C# AI的智障行为http服务—东方仙盟练气期
开发语言·http·c#
缺点内向1 天前
C#中如何创建目录(TOC):使用Spire.Doc for .NET实现Word TOC自动化
c#·自动化·word·.net
2301_816997881 天前
Word 创建打开与保存文档
c#·word·xhtml