locked
Setting animation speed, not duration RRS feed

  • Question

  • I have a textblock inside a canvas that is inside a border that is inside a grid cell (phew!). What I am attempting to do is to scroll the text through the cell when the text is too long to fit inside the cell.
    I can do this quite easily if I want to have the text start off the screen and scroll past and off the other side like this:

    <Storyboard RepeatBehavior="Forever"
        <DoubleAnimation Storyboard.TargetName="scrollTranslate" 
                         Storyboard.TargetProperty="X" 
                         Duration="0:0:8" 
                         From="{Binding ElementName=textBorder, Path=ActualWidth}" 
                         To="{Binding ElementName=sampleText, Path=ActualWidth, Converter={StaticResource negate}}"/> 
    </Storyboard> 

    The "negate" converter just takes a value and returns minus that value, so the text scrolls from the edge of the border (the textBorder element) to a point at minus the length of the string (the sampleText element).

    The problem with this is that I'd prefer to have the text start at zero, scroll off the left-hand side and then reappear on the right. To do this, I think I need two separate double animations like this:

    <Storyboard RepeatBehavior="Forever"
        <DoubleAnimation Storyboard.TargetName="scrollTranslate"  
                         Storyboard.TargetProperty="X" 
                         Duration="0:0:8" 
                         From="0"  
                         To="{Binding ElementName=sampleText, Path=ActualWidth, Converter={StaticResource negate}}"/> 
        <!-- TO DO: Calculate the duration so scrolling is the same speed --> 
        <DoubleAnimation Storyboard.TargetName="scrollTranslate"  
                         Storyboard.TargetProperty="X" 
                         BeginTime="0:0:8" 
                         Duration="0:0:8" 
                         From="{Binding ElementName=textBorder, Path=ActualWidth}"  
                         To="0"/> 
    </Storyboard> 

    So here, I first scroll from 0 to minus the length of the text and then from the edge of the border to 0. The problem is that the distances traveled by the two animations is not necessarily equal, so they will not necessarily run at the same speed because I can only set duration. Is there anyway in XAML to get around this problem? Or is the only alternative to calculate the duration in code behind?
    It seems to me like there really are situations (like this one) where setting the duration isn't enough control over the speed of an animation, if the distance isn't constant.


    • Edited by wjousts Tuesday, July 1, 2008 1:15 PM typo
    Tuesday, July 1, 2008 1:13 PM

All replies

  • Maybe DoubleAnimationUsingKeyFrames with descrete or values or even splines could help you.

    Benjamin
    Wednesday, July 23, 2008 7:19 PM