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);
相关推荐
Poetinthedusk2 天前
WPF应用跟随桌面切换
开发语言·wpf
小北方城市网2 天前
MongoDB 分布式存储与查询优化:从副本集到分片集群
java·spring boot·redis·分布式·wpf
听麟2 天前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
笨蛋不要掉眼泪3 天前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf
LcVong4 天前
WPF MediaPlayer获取网络视频流当前帧并展示图片完整范例
网络·wpf
bugcome_com4 天前
WPF数据绑定入门:从传统事件到5种绑定模式
wpf
LateFrames4 天前
我用 WPF 做了一个 “苍蝇飞舞” 的屏保
ui·wpf
wuty0074 天前
完善基于WPF开发的标尺控件(含实例代码)
wpf·wpf标尺·支持横向竖向标尺·ruler
浩浩测试一下5 天前
洪水猛兽攻击 Ddos Dos cc Drdos floods区别
安全·web安全·网络安全·系统安全·wpf·可信计算技术·安全架构
无心水5 天前
分布式环境下定时任务与SELECT FOR UPDATE的陷阱与解决方案
分布式·后端·wpf·xxl-job·quartz·定时任务·selectforupdate