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);
相关推荐
爱吃烤鸡翅的酸菜鱼1 天前
Java 事件发布-订阅机制全解析:从原生实现到主流中间件
java·中间件·wpf·事件·发布订阅
武藤一雄2 天前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
CSharp精选营2 天前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
baivfhpwxf20232 天前
wpf TextBlock 控件如何根据内容换行?
wpf
亘元有量-流量变现2 天前
鸿蒙、安卓、苹果音频设备技术深度解析与开发实践
android·wpf·harmonyos·亘元有量·积分墙
软泡芙2 天前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
浪扼飞舟2 天前
WPF输入验证(ValidationRule)
java·javascript·wpf
IOFsmLtzR4 天前
Flink Agents 源码解读 --- (5) --- ActionExecutionOperator
microsoft·flink·wpf
廋到被风吹走5 天前
【AI】Codex 复杂任务拆解:从“一气呵成“到“步步为营“
人工智能·wpf
希望永不加班5 天前
SpringBoot 整合 Redis 缓存
spring boot·redis·后端·缓存·wpf