目录

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);