Wired animation and two-way snap problem

I play with WPF animations and run into some kind of wired problem.

I have Slider and TextBox. The TextBox is bound to Slider.Value using a two-way binding:

<StackPanel> <Slider x:Name="MySlider" Minimum="0" Maximum="100" Value="50" /> <TextBox Text="{Binding ElementName=MySlider, Path=Value, Mode=TwoWay}" /> <Button Click="Button_Click">Test</Button> </StackPanel> 

When I drag the slider, the text in the text box changes. When I change the text in the text box, the value of the slider is updated, it works correctly.

Now I am adding an animation that animates the Slider.Value property to 0. I start it when I click the button.

  private void Button_Click(object sender, RoutedEventArgs e) { Storyboard storyBoard = new Storyboard(); DoubleAnimation animation = new DoubleAnimation(); animation.Duration = new Duration(TimeSpan.FromSeconds(0.5)); animation.To = 0; Storyboard.SetTarget(animation, MySlider); Storyboard.SetTargetProperty(animation, new PropertyPath(Slider.ValueProperty)); storyBoard.Children.Add(animation); storyBoard.Begin(); } 

When I click the button, the animation scrolls the slider to 0. The TextBox also changes to 0 synchronously with the slider.

And now I'm facing a problem. After the animation, I cannot change the text in the text box. I am changing the text, moving the focus and the text, decreasing the value of the slider to 0. I can still move the slider and updating the text field with the value of the slider. But I can not set the value of the slider using a text box.

I think that when the animation stops, the value somehow depends on the value specified in the animation. For a property, but I can't figure out how to unfreeze it. Or maybe this is something else?

thanks

+4
source share
1 answer

This is due to the priority of the value of the dependency property, which means that the value set by the animation has a higher "priority" than the value set through the binding.

Here is a quote from MSDN on how to do this:

One way to regain control of an animated property in your code is to use the BeginAnimation method and specify null for the AnimationTimeline parameter. For more information and an example, see How to set the property After animation with a storyboard .

+3
source

Source: https://habr.com/ru/post/1344822/


All Articles