WPF: Change the RotateTransform center before angle animation

user3364152 picture user3364152 · Apr 2, 2014 · Viewed 7.3k times · Source

I want to rotate an animation for my user control which would be triggered when the control gets the keyboard focus. The animation should rotate it around its center.

The problem is, I set the original center point on the lower left corner of the control for the loaded animation. So, to correct that, I set the center in the middle of the control on the trigger IsFocused.

But my GetKeyboardFocus animation keep the original center.

Is the trigger executed after the eventtrigger? Or I'm doing something wrong.

<UserControl x:Class="testtuile.rectangle"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Height="150" Width="300" Padding="4" Focusable="True" MouseDown="UserControl_MouseDown" IsTabStop="True">
<UserControl.RenderTransform>
    <RotateTransform Angle="0" CenterX="0" CenterY="150"></RotateTransform>
</UserControl.RenderTransform>
<UserControl.Style>
    <Style>
        <Style.Triggers>
            <Trigger Property="Control.IsFocused" Value="True">
                <Setter Property="Control.BorderBrush" Value="Gold"></Setter>
                <Setter Property="Control.BorderThickness" Value="2"></Setter>
                <Setter Property="Control.RenderTransform">
                    <Setter.Value>
                        <RotateTransform CenterX="150" CenterY="75"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>
<UserControl.Triggers>
    <EventTrigger RoutedEvent="Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
                        From="90" To="0" Duration="0:0:0.8" 
                        AutoReverse="False" 
                    />
                    <DoubleAnimation
                        Storyboard.TargetProperty="Opacity"
                        From="0" Duration="0:0:0.6"
                        AutoReverse="False"
                    />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="GotKeyboardFocus">
        <EventTrigger.Actions>
            <BeginStoryboard Name="ButtonFocusedAnimation">
                <Storyboard>
                    
                    <DoubleAnimation
                        Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
                        From="-2" To="2" Duration="0:0:1" 
                        AutoReverse="True" RepeatBehavior="Forever" 
                    />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="LostKeyboardFocus">
        <StopStoryboard BeginStoryboardName="ButtonFocusedAnimation" />
    </EventTrigger>
</UserControl.Triggers>

<Grid Background="Aquamarine">


</Grid>

Thank you for your help.

Answer

dkozl picture dkozl · Apr 2, 2014

You don't have to change it before animation. To rotate round centre point set UIElement.RenderTransformOrigin against UserControl:

<UserControl RenderTransformOrigin="0.5,0.5" ...>

Gets or sets the center point of any possible render transform declared by RenderTransform, relative to the bounds of the element

and later

RenderTransformOrigin has a somewhat nonstandard use of the Point structure value, in that the Point does not represent an absolute location in a coordinate system. Instead, values between 0 and 1 are interpreted as a factor for the range of the current element in each x,y axis. For example, (0.5,0.5) will cause the render transform to be centered on the element, or (1,1) would place the render transform at the bottom right corner of the element