详细介绍:WPF 控制动画开关

记录一种实现方式:

第一步:

首先定义一个静态类,提供依赖属性,进而方便在xaml中实现绑定:

public static class AnimationBehavior
{
// 定义附加属性
public static readonly DependencyProperty IsAnimatingProperty =
DependencyProperty.RegisterAttached(
"IsAnimating",
typeof(bool),
typeof(AnimationBehavior),
new PropertyMetadata(false, OnIsAnimatingChanged));
public static bool GetIsAnimating(DependencyObject obj) =>
(bool)obj.GetValue(IsAnimatingProperty);
public static void SetIsAnimating(DependencyObject obj, bool value) =>
obj.SetValue(IsAnimatingProperty, value);
private static void OnIsAnimatingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is FrameworkElement element)
{
// 通过 VisualTreeHelper 查找模板中的 Storyboard
if (element.FindResource("ShadowAnimation") is Storyboard storyboard)
{
if ((bool)e.NewValue)
storyboard.Begin(element, true); // 启动动画
else
storyboard.Stop(element);      // 停止动画
}
}
}
}

其中实现了依赖属性IsAnimating。

第二步:

在前端中定义动画:

-->

其中每一个DoubleAnimation定义中的TargetName是当前界面的其他对象名称:

比如myBorder 和 ShadowEffect; 

第三步:

创建绑定关系:

比如上方代码中:

localVM:AnimationBehavior.IsAnimating="{Binding CurrentActionHandle.ExecutionState, Converter={StaticResource SingleFlowExecutionStateToBoolJustRunnimg}}"

给新手解释一下,其中localVM是自己定义的命名空间:

可我这里使用了转换器,可以理解为就是把执行动画的开关绑定到了我后端ViewModel的属性上。

posted @ 2025-07-26 22:18  wzzkaifa  阅读(18)  评论(0)    收藏  举报