1

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

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

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

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

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.

Tim Kathete Stadler
  • 1,067
  • 4
  • 26
  • 52
  • I deleted my answer because i miss the problem. One way binding broken doesn't seems the case. – Giangregorio Sep 29 '15 at 09:35
  • 1
    please clarify your question, why do you want to add animation in code while it already works via setting up in XAML? Of course once you setup animation in code all values set via XAML will be overwritten. See this [WPF Dependency Property Value Precedence](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CBwQFjAAahUKEwi5_sXbgZzIAhUkGKYKHToNAsM&url=https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fvstudio%2Fms743230(v%3Dvs.100).aspx&usg=AFQjCNGMSO4IIv2O0gOK0YvJ8mQRqAFZBQ&sig2=NoPktI1J7mw2XmnM_ED5Ag&bvm=bv.103388427,bs.1,d.dGY) – Hopeless Sep 29 '15 at 10:17

1 Answers1

7

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.

Clemens
  • 123,504
  • 12
  • 155
  • 268