简体   繁体   English

* any * wpf控件的动画背景不透明度

[英]Animate background opacity of *any* wpf control

I want an effect where I can animate the background opacity of any GroupBox control. 我想要一种可以使任何GroupBox控件的背景不透明的动画效果。
This would give me additional visual notification if something changes - I can flash the control to indicate the new status. 如果发生某些更改,这将给我额外的视觉通知-我可以闪烁控件以指示新状态。

I have 我有

private void ToggleBackgroundFade(Control control)
{
    var duration = new TimeSpan(0, 0, 0, 0, 200);

    var animation = new DoubleAnimation { From = 1.0, To = 0.0, Duration = new Duration(duration) };
    // new brush because old one is frozen
    control.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xD6, 0xEF, 0xD1));
    control.Background.BeginAnimation(OpacityProperty, animation);
}

private void KickoffFlashingAnimation(Control controlToFade)
{
    controlToFade.Dispatcher.BeginInvoke(new Action(() => 
        ToggleBackgroundFade(controlToFade))
    );
}

and all with style 还有所有风格

<Style x:Key="GroupBoxHeader" TargetType="{x:Type GroupBox}">
    ...
    <Setter Property="Background" Value="#00D6EFD1" />
</Style>

so I want to pass any GroupBox to the KickoffFlashingAnimation() method. 所以我想将任何GroupBox传递给KickoffFlashingAnimation()方法。

It runs fine, with no errors, but the groupbox doesn't change color after initially being set to the light green #ffd6efd1.. 它运行正常,没有错误,但是在最初设置为浅绿色#ffd6efd1后,groupbox不会更改颜色。
Any tips? 有小费吗?

嗨,我已经尝试了上面的代码,并且对我来说对Groupbox来说效果很好.Groupbox可能存在问题,即它的所有Content区域都填充了其他控件,这些控件最终隐藏了Background.So,只需对其进行测试,就可以给您的GroupBox内容比控件中的宽度更大,然后尝试通过传递GroupBox的实例来运行上面的动画代码,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM