动画打破绑定 [英] Animation breaks binding

查看:81
本文介绍了动画打破绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox ,其 Opacity 属性具有以下绑定:

I have a ComboBox whose Opacity property has the following binding:

Opacity="{Binding ElementName=stackPanel, Path=IsMouseOver, Converter={StaticResource mouseOverConverter}}"

基本上,如果 IsMouseOver 属性为true,则 ComboBox 不透明度为1,否则为0.4。

Basically, if the IsMouseOver property is true, the ComboBox has an Opacity of 1, otherwise 0.4.

现在我将此动画应用于 ComboBox

Now I apply this animation to the ComboBox:

private void AnimateComboBox()
{
  DoubleAnimation da = new DoubleAnimation();
  da.From = 0.4;
  da.To = 1;
  da.Duration = TimeSpan.FromSeconds(0.8);
  da.AutoReverse = true;

  ComboClassList.BeginAnimation(ComboBox.OpacityProperty, da);  
}

效果很好,但随后绑定 ComboBox 不再起作用。当我将鼠标移到 StackPanel 上时,不透明度不会改变。为什么动画会破坏我的束缚? Snoop说,绑定仍然存在,只是在Snoop中以红色突出显示。

That works well, but afterwards the binding of the ComboBox doesn't work anymore. The Opacity doesn't change when I move my mouse over the StackPanel. Why does the animation break my binding? Snoop says, the binding still exists, altough it's highlighted red in Snoop.

推荐答案

默认情况下,动画保留最终属性值。要更改此设置,请将其 FillBehavior 属性设置为 Stop

The animation is by default holding the final property value. To change that, set its FillBehavior property to Stop:

var animation = new DoubleAnimation
{
    From = 0.4,
    To = 1,
    Duration = TimeSpan.FromSeconds(0.8),
    AutoReverse = true,
    FillBehavior = FillBehavior.Stop
};

动画结束时,属性将重新设置为绑定提供的值。

When the animation ends, the property will be set back to the value provided by the binding.

这篇关于动画打破绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆