1

I'm trying to change duration of DoubleAnimation control by Slider's value but it does not get effect. I don't know why?

Here's my code:

XAML:

<Border.Triggers>
    <EventTrigger RoutedEvent="Loaded">
       <BeginStoryboard>
           <Storyboard Storyboard.TargetName="borUpperTranslate" Storyboard.TargetProperty="X">
               <DoubleAnimation Name="dbaUpperTranslate" From="418" To="230" Duration="0:0:1" RepeatBehavior="Forever" AutoReverse="True"/>
            </Storyboard>
       </BeginStoryboard>
    </EventTrigger>

C#:

 private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Slider sl = sender as Slider;
            dbaUpperTranslate.Duration = new Duration(TimeSpan.FromSeconds(sl.Value));
        }

Any help would be appreciate! Thanks in advance.

4

1 回答 1

3

RepeatBehavior="Forever" set to True, so this animation will run forever which gets initiated with duration set to 0:0:1.

If you remove RepeatBehavior="Forever" and hook on an event say MouseLeftButtonDown, it works as expected.


However, in case you want to update it with RepeatBehavior="Forever", you need to stop animation first, set duration and than restart animation.

Set name on storyBoard:

<Storyboard Storyboard.TargetName="borUpperTranslate"
            Storyboard.TargetProperty="X" Name="storyboard">

And in handler:

Slider sl = sender as Slider;
storyboard.Stop();
dbaUpperTranslate.Duration = new Duration(TimeSpan.FromSeconds(sl.Value));
storyboard.Begin();

Moreover, if you don't want jerky look, you should consider removing From attribute, so it will resume from where it left before stopping animation.

于 2013-12-24T05:32:57.507 回答