locked
StoryBoard

    Question

  • Hello ,

    Is there any way of determining the current position of an object(image) which is under an storyboard animation.

    Actually i need a trail effect on a object in 2-D motion.

    Any suggestions would be grateful.

    Monday, August 11, 2014 8:04 AM

Answers

  • Storyboard animations change property values so you can get the current position by accessing the property that is changed by the animation. For example, consider the following XAML markup where the Width of a Button is animated from 10 to 500 during 10 seconds:

    <StackPanel>
                            <Button x:Name="button" Background="Green" Content="Button..." Width="10" Height="100" Opacity="0"/>
                            <TextBlock Text="{Binding Path=Width, ElementName=button}"/>
                            <StackPanel.Triggers>
                                <EventTrigger RoutedEvent="StackPanel.Loaded">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation EnableDependentAnimation="True" To="500" 
                                                             Storyboard.TargetName="button" Storyboard.TargetProperty="Width" 
                                                             Duration="0:0:10"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </StackPanel.Triggers>
                        </StackPanel>

    You can get the current width of the Button during the animation by simply access its Width property as usual, i.e.:

    <Button Content="Get Width" Click="Button_Click"/>

    private void Button_Click(object sender, RoutedEventArgs e)
            {
               double currentWidth = button.Width;
            }
    

    Monday, August 11, 2014 8:25 AM
  • If you're moving an object with animation I assume you're using TranslateTransform or CompositeTransform, then it's easy to retrieve the position of the object as it move from the X & Y properties.

    I hope it helps,

    Yassine

    Monday, August 11, 2014 8:27 AM

All replies

  • Storyboard animations change property values so you can get the current position by accessing the property that is changed by the animation. For example, consider the following XAML markup where the Width of a Button is animated from 10 to 500 during 10 seconds:

    <StackPanel>
                            <Button x:Name="button" Background="Green" Content="Button..." Width="10" Height="100" Opacity="0"/>
                            <TextBlock Text="{Binding Path=Width, ElementName=button}"/>
                            <StackPanel.Triggers>
                                <EventTrigger RoutedEvent="StackPanel.Loaded">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation EnableDependentAnimation="True" To="500" 
                                                             Storyboard.TargetName="button" Storyboard.TargetProperty="Width" 
                                                             Duration="0:0:10"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </StackPanel.Triggers>
                        </StackPanel>

    You can get the current width of the Button during the animation by simply access its Width property as usual, i.e.:

    <Button Content="Get Width" Click="Button_Click"/>

    private void Button_Click(object sender, RoutedEventArgs e)
            {
               double currentWidth = button.Width;
            }
    

    Monday, August 11, 2014 8:25 AM
  • If you're moving an object with animation I assume you're using TranslateTransform or CompositeTransform, then it's easy to retrieve the position of the object as it move from the X & Y properties.

    I hope it helps,

    Yassine

    Monday, August 11, 2014 8:27 AM