Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate a grid to move or slide to the right WPF

I have a grid with elements in it. I want to move all of those elements, so is it not possible to just move the grid all together? This isn't having any effect from what I'm seeing and I've tried even ScaleX and such as well.

<Grid x:Name="HancockDetails" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Grid.Column="1" RenderTransformOrigin="0.5,0.5">                       
   <Rectangle HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100"/>
    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Foreground="#FFF30000"/>               
            </Grid>
          </Grid>
      </s:ScatterViewItem>

My function:

Storyboard sb = new Storyboard();

DoubleAnimation slide = new DoubleAnimation();
slide.To = 3000.0;
slide.From = 0;
slide.Duration = new Duration(TimeSpan.FromMilliseconds(40.0));

// Set the target of the animation
Storyboard.SetTarget(slide,HancockDetails);
Storyboard.SetTargetProperty(slide, new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));

// Kick the animation off
sb.Children.Add(slide);
sb.Begin();
like image 637
user99999991 Avatar asked Jul 19 '13 18:07

user99999991


2 Answers

You need to first add the RenderTransform in the markup, otherwise the runtime can't find it:

<Grid x:Name="HancockDetails" ...>  
    <Grid.RenderTransform>
        <TranslateTransform />
    </Grid.RenderTransform>
</Grid>

I assume you want to use TranslateTransform, and not ScaleTransform, since the goal is to slide the grid over, not to resize it?

Storyboard.SetTargetProperty(slide, 
    new PropertyPath("RenderTransform.(TranslateTransform.X)"));
like image 55
McGarnagle Avatar answered Sep 24 '22 19:09

McGarnagle


Instead of trying to create a storyboard by hand it is muuuuuuch easier to use Expression Blend. Its a free design tool that compliments Visual Studio. The storyboard you are explaining would take all of 30 seconds to make there and there would be no code. Then when you wanted it to run it would just be the StoryBoardName.Begin()

To easy right? Heres a resource. http://msdn.microsoft.com/en-us/expression/cc197141.aspx

like image 28
Anthony Russell Avatar answered Sep 21 '22 19:09

Anthony Russell