Skip to main content

Microsoft Silverlight

Answered Question Quick and easy question - RotateTransformRSS Feed

(0)

cjhutchings
cjhutchings

Member

Member

79 points

85 Posts

Quick and easy question - RotateTransform

Hi, this is a simple thing that's driving me crazy.

 How in the world do I rotate an object on a click?

XAML:
<Rectangle Width="100" Height="100" Fill="#FF000000" MouseLeftButtonDown="Click">
    <Rectangle.RenderTransform>
        <TransformGroup>
            <ScaleTransform />
            <SkewTransform />
            <RotateTransform Angle="90" />
            <TranslateTransform />
        </TransformGroup>
    </Rectangle.RenderTransform>
</Rectangle>

C#:
private void Click(object sender, MouseButtonEventArgs e)
{
    ?????? (Just want it to change the angle from 90 to 0)
}

clint1222
clint1222

Participant

Participant

1542 points

216 Posts

Re: Quick and easy question - RotateTransform

Hi,

You could create a storyboard and call it in your Click Method.

<Storyboard x:Name="sbRotate">

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">

<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>

</DoubleAnimationUsingKeyFrames>

</Storyboard>

private void Click(object sender, MouseButtonEventArgs e)
{
    sbRotate.Begin();
}

cjhutchings
cjhutchings

Member

Member

79 points

85 Posts

Re: Quick and easy question - RotateTransform

Yeah, I just didn't want to add a bunch more storyboards to my XAML.  I know that it's possible to change the property in C#, I'm just having trouble figuring it out.  Plus, I didn't want to have it animate, just change.  (I realize I could mimic this by having the KeyTime="00:00:00.001", so I might end up having to do that if I can't find a C# solution soon).

 Thanks for the reply.

clint1222
clint1222

Participant

Participant

1542 points

216 Posts

Answered Question

Re: Quick and easy question - RotateTransform

Hi,

Give your transform an x:name

<RotateTransform x:Name="myRotateTransform" Angle="90" />

In your click event set it to 0.

myRotateTransform.Angle = 0;

cjhutchings
cjhutchings

Member

Member

79 points

85 Posts

Re: Quick and easy question - RotateTransform

Thanks Clint,

 That makes a lot of sense, and I can see why it should work, but it's not for me.  I'm wondering if it doesn't work the same for a Canvas?  I'm including code so it's hopefully more clear.  Hopefully, it doesn't confuse things.

XAML:
<Canvas Height="643" Width="1517" Canvas.Left="-466.5" Canvas.Top="-16.5" x:Name="OEM" RenderTransformOrigin="0.5,1">
           <Canvas.RenderTransform>
            <TransformGroup>
             <ScaleTransform/>
             <SkewTransform/>
             <RotateTransform x:Name="oemRot" Angle="0" />
             <TranslateTransform/>
            </TransformGroup>
           </Canvas.RenderTransform>
           <Path Height="643" Width="1517" Fill="#FF024981" Stretch="Fill" Stroke="{x:Null}" StrokeThickness="3" Data="M-464,-14 L1050,-14 L296,626 z"/>
           <TextBlock Canvas.Left="726.5" Canvas.Top="17.5" FontFamily="Portable User Interface" FontSize="24" FontWeight="Bold" Foreground="#FFFFFFFF" Text="Title" TextWrapping="Wrap"/>
           <TextBlock Text="Text." FontFamily="Segoe.zip#Segoe Bold" TextWrapping="Wrap" FontSize="10" Foreground="#FFFFFFFF" Canvas.Left="693" Canvas.Top="142" Width="135.5"/>
           <TextBlock Width="158.65" FontSize="10" Foreground="#FFFFFFFF" Text="Text." TextWrapping="Wrap" Canvas.Left="516" Canvas.Top="259"/>
           <TextBlock Width="154.44" FontSize="10" Foreground="#FFFFFFFF" Text="Text." TextWrapping="Wrap" Canvas.Left="845" Canvas.Top="259"/>
           <TextBlock Width="146.94" FontSize="10" Foreground="#FFFFFFFF" Text="Text." TextWrapping="Wrap" Canvas.Left="572" Canvas.Top="383"/>
           <TextBlock Width="122.70" FontSize="10" Foreground="#FFFFFFFF" Text="Text." TextWrapping="Wrap" Canvas.Left="821" Canvas.Top="390"/>
           <TextBlock Width="133.47" FontSize="10" Foreground="#FFFFFFFF" Text="Text." TextWrapping="Wrap" Canvas.Left="692" Canvas.Top="508"/>
          </Canvas>

C#:
private void textBlock5_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            number.Text = "1";
            textBlock5_Copy.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock5_Copy.FontWeight = FontWeights.Normal;
            textBlock6.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock6.FontWeight = FontWeights.Normal;
            textBlock7.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock7.FontWeight = FontWeights.Normal;
            textBlock8.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock8.FontWeight = FontWeights.Normal;
            textBlock9.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock9.FontWeight = FontWeights.Normal;
            textBlock10.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock10.FontWeight = FontWeights.Normal;
            textBlock10_Copy.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            textBlock10_Copy.FontWeight = FontWeights.Normal;
            textBlock5.Foreground = new SolidColorBrush(Color.FromArgb(255, 2, 73, 129));
            textBlock5.FontWeight = FontWeights.Bold;
            oemRot.Angle = 0;
            openRot.Angle = -100;
            selectRot.Angle = -100;
            entRot.Angle = -100;
            campusRot.Angle = -100;
            retailRot.Angle = -100;
            refurbRot.Angle = -100;
        }

So, not to confuse any more deeply, i have a storyboard that's run and made the canvas' angle at 95 degrees.  So when the button is clicked the canvas is at an angle of 95 degrees, not 0.

 ----Edit----

It works before any animation is used, but doesn't work properly after a storyboard has been run.  I'm thinking that after changing the rotation through a storyboard, it doesn't want to "read" the new rotation this way?  Does that make sense?

cjhutchings
cjhutchings

Member

Member

79 points

85 Posts

Re: Re: Quick and easy question - RotateTransform

I went ahead and made storyboards of the rotate with KeyTimes of 0.001 seconds.  I know it's not the best way to go about it, but i'm in a hurry and need to get this working.  Thanks again for the help on this Clint, and if anyone has any input I would love to know the solution through C# for future endeavors.

Thanks!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities