PDA

View Full Version : WPF: How to repeat rotate transform



psychman
February 17th, 2009, 02:57 AM
Hello. I have created a context menu with two options, rotate image 90 degrees clockwise and rotate image 90 degrees counterclockwise. The method I am using to rotate works the first time, but will not continue to rotate the object, in the same direction, on subsequent tries. I want the user to be able to rotate the image in 90 degree increments until the image has gone around 360 degrees, if need be. Here is what I have tried:

private void RotateCW (object sender, RoutedEventArgs e)
{
RotateTransform cwRotateTransform = new RotateTransform(); View2.RenderTransform = cwRotateTransform;
if (cwRotateTransform.Angle == 360)
cwRotateTransform.Angle=0;
else cwRotateTransform.Angle +=90;
}


Thanks in advance for any help on this.

kirupa
February 17th, 2009, 03:57 AM
Hey psychman,
You need to assign your rotatetransform to your object's transformgroup. It's a bit convulted and I wish it were easier, but here is how you can accomplish this for an element called blueSquare:


private int currentAngle = 0;
private void RotateSquare(object sender, RoutedEventArgs e)
{
RotateTransform rotateTransform = new RotateTransform();
currentAngle += 45;

rotateTransform.Angle = currentAngle;

TransformGroup transformGroup = new TransformGroup();
transformGroup.Children.Add(rotateTransform);

blueSquare.RenderTransform = transformGroup;
}


:)

psychman
February 17th, 2009, 05:16 PM
Thanks for the help Kirupa. It works great now!