I have a Button. For some reason, I would like to apply transforms in code behind. And The transforms work fine. But when I try to add animations on the transforms, the animations don't take effect. My code is as below.
<Grid x:Name="grid">
<Button Height="40" Width="120" Content="Button"
x:Name="button" Click="button_Click">
</Button>
</Grid>
and in codebehind when button Clicked is :
private void button_Click(object sender, RoutedEventArgs e)
{
var animation = new DoubleAnimation(0, 20, new Duration(TimeSpan.FromSeconds(1)));
var story = new Storyboard();
story.Children.Add(animation);
Storyboard.SetTarget(animation, button);
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));
story.Begin();
}
In order to animate a TranslateTransform, there has to be one assigned to the Button's
RenderTransformorLayoutTransformproperty, either in XAMLor in code behind
Then you do not need a Storyboard, because you can directly animate the transform like
With a Storyboard, you would have to set the correct property path: