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);
相关推荐
软泡芙8 小时前
【WPF 】MVVM 设计模式在 WPF 中的实战应用
设计模式·wpf
张小俊_10 小时前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
七夜zippoe1 天前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang1 天前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com2 天前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf
武藤一雄2 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
炸炸鱼.2 天前
ELK 企业级日志分析系统完整部署手册
elk·wpf
Mr_pyx3 天前
微服务可观测性实战:分布式链路追踪从入门到精通
wpf
c#上位机4 天前
wpf附加事件
wpf
玖笙&4 天前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio