Reputation: 15
I want to rotate simple grid with storyboard in c# code in wpf my xaml code is
<Window x:Class="rotate_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:rotate_test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid Name="my_grid">
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="150,170,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</Grid>
</Window>
that have grid and one button in it in onclick event for button I use
Storyboard storyboard = new Storyboard();
DoubleAnimation rotateAnimation = new DoubleAnimation()
{
From = 0,
To = 360,
Duration = new Duration(TimeSpan.FromSeconds(10.0))
};
Storyboard.SetTarget(rotateAnimation, my_grid);
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
storyboard.Children.Add(rotateAnimation);
storyboard.Begin();
but it dose not work what is my problem? how can I set Acceleration for animation? thnx
Upvotes: 1
Views: 2602
Reputation: 128013
You have to initialize the Grid's RenderTransform
before you can animate it.
<Grid Name="my_grid">
<Grid.RenderTransform>
<RotateTransform />
</Grid.RenderTransform>
</Grid>
You could also directly animate the RotateTransform
without a Storyboard.
Just give it a Name
<RotateTransform x:Name="transform" />
and animate it in code behind like
transform.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
Besides that, DoubleAnimation has several properties that control acceleration and deceleration, like AccelerationRatio
, DecelerationRatio
and EasingFunction
.
Upvotes: 2