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);
相关推荐
LateFrames13 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验
heimeiyingwang1 天前
【架构实战】日志体系ELK:集中化日志管理实践
elk·架构·wpf
CPU不够了1 天前
WPF 多选下拉+搜索过滤_wpf下拉选项增加搜索
wpf
FuckPatience1 天前
WPF 列表控件自动拉伸子元素的宽度
wpf
LCG元1 天前
【Go后端开发】从 0 到生产级:高性能分布式网关全实现 + 接口限流熔断降级实战
分布式·golang·wpf
枫叶林FYL2 天前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
她说彩礼65万2 天前
WPF 多值转换器
wpf
无心水2 天前
【分布式利器:金融级】金融级分布式架构开源框架全景解读
人工智能·分布式·金融·架构·开源·wpf·金融级框架
她说彩礼65万2 天前
WPF 转换器
wpf
WPF工业上位机3 天前
匠心研智造,同心赴新程-WPF硬件通讯之串口&Socket
wpf