wpf给Border添加闪烁边框

目录

xaml页面设置样式,不是必须,下面的是后台代码设置的动画样式

xml 复制代码
        <!--  闪烁动画资源  -->
        <Storyboard
            x:Key="BlinkStoryboard"
            AutoReverse="True"
            RepeatBehavior="Forever">
            <!--  从红色闪到透明,可根据需求修改颜色  -->
            <ColorAnimation
                Storyboard.TargetProperty="BorderBrush.Color"
                From="Magenta"
                To="Transparent"
                Duration="0:0:0.2" />
        </Storyboard>

边框闪烁

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Module.PEIS.ZhiYeBingTiJian.ViewModel
{
    /// <summary>
    /// 控件事件处理;样式处理;边框闪烁
    /// </summary>
    /// 创建时间:2026-1-14 16:58:23,wanghaoli
    internal class FrameworkElementStyleHandle
    {
        private Storyboard _flashStoryboard;

        public FrameworkElementStyleHandle()
        {
            // 创建颜色动画(透明<->红色)
            ColorAnimation colorAnim = new ColorAnimation
            {
                From = Colors.Transparent,
                To = Colors.Magenta,
                Duration = new Duration(TimeSpan.FromMilliseconds(200)),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };

            // 创建Storyboard
            _flashStoryboard = new Storyboard();
            //Storyboard.SetTarget(colorAnim, FlashBorder);

            Storyboard.SetTargetProperty(colorAnim, new PropertyPath("BorderBrush.Color"));
            _flashStoryboard.Children.Add(colorAnim);

        }


        /// <summary>
        /// 添加动画,边框闪烁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AddFlicker(FrameworkElement targetBorder)
        {
            //Border targetBorder
            if (targetBorder == null)
            {
                return;
            }
            if (targetBorder is Border targetBorder2)
            {
                //添加动画,边框闪烁
                //var blinkStoryboard = (Storyboard)FindResource("BlinkStoryboard");
                //blinkStoryboard.Begin(targetBorder2, true);
                _flashStoryboard.Begin(targetBorder2, true);

                targetBorder2.MouseEnter -= Remove_flicker;
                targetBorder2.MouseEnter += Remove_flicker;
            }
        }

        /// <summary>
        /// 移除闪烁动画
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Remove_flicker(object sender, MouseEventArgs e)
        {
            var border = sender as Border;
            if (border == null) return;

            // 停止并移除闪烁动画
            //var blinkStoryboard = (Storyboard)FindResource("BlinkStoryboard");
            //blinkStoryboard.Stop(border);
            _flashStoryboard.Stop(border);
            border.BeginAnimation(Border.BorderBrushProperty, null);

            // 恢复Border的原始边框颜色(可选,根据需求调整)
            border.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent);
        }


    }
}

调用

csharp 复制代码
var element = (this.FindName(msg) as FrameworkElement);
element?.Focus();
element?.BringIntoView();

//添加动画,边框闪烁
FrameworkElementStyleHandle elementStyleHandle = new FrameworkElementStyleHandle();
elementStyleHandle.AddFlicker(element);
相关推荐
myenjoy_15 小时前
大规模采集架构——从单台网关到千点集群
架构·wpf
Chris _data10 小时前
c#学习WPF笔记(一)
学习·c#·wpf
FuckPatience1 天前
WPF 自定义容器控件的布局
wpf
逆境不可逃1 天前
深入理解 SingleFlight:从单机到分布式的请求合并方案全解析
分布式·wpf
TDengine (老段)1 天前
TDengine 逻辑计划生成 — 从 AST 到关系代数算子树
大数据·数据库·物联网·wpf·时序数据库·tdengine·涛思数据
小二·2 天前
微服务架构设计与实践
微服务·架构·wpf
暖馒2 天前
WPF-Prism学习入门步骤记录
学习·wpf
baivfhpwxf20232 天前
雷赛(Leadshine)EtherCAT 数字 I/O 模块(如 EMC-E5064-8)的状态指示灯(I/O 状态)说明
c#·wpf
故渊at3 天前
第二板块:Android 四大组件标准化学理 | 第十二篇:四大组件全景总结与系统服务(System Server)架构
android·架构·wpf·四大组件·system service
伶俜663 天前
# [特殊字符] 零基础学 ArkUI 数据持久化(专题三):5 种存储方案深度对比
学习·华为·wpf·harmonyos