问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/32839906/animation-breaks-binding