Udostępnij za pośrednictwem


How to: Specify the Origin of a Transform by Using Relative Values

This example shows how to use relative values to specify the origin of a RenderTransform that is applied to a FrameworkElement.

When you rotate, scale, or skew a FrameworkElement by using the RenderTransform property, the default setting applies the transform to the upper-left corner of the element. If you want to rotate, scale, or skew from the center of the element, you can compensate by setting the center of the transform to the center of the element. However, that solution requires that you know the size of the element. An easier way of applying a transform to the center of an element is to set its RenderTransformOrigin property to (0.5, 0.5), instead of setting a center value on the transform itself.

Example

The following example uses a RotateTransform to rotate a Button 45 degrees clockwise. Because the example does not specify a center, the button rotates about the point (0, 0), which is its upper-left corner. The RotateTransform is applied to the RenderTransform property.

The following illustration shows the transformation result for the example that follows.

A 45 degree clockwise rotation by using the RenderTransform property

A button transformed using RenderTransform

<Border Margin="30" 
  HorizontalAlignment="Left" VerticalAlignment="Top"
  BorderBrush="Black" BorderThickness="1" >
  <StackPanel Orientation="Vertical">
    <Button Content="A Button" Opacity="1" />
    <Button Content="Rotated Button">
      <Button.RenderTransform>
        <RotateTransform Angle="45" />
      </Button.RenderTransform>
    </Button>
    <Button Content="A Button" Opacity="1" />
  </StackPanel>
</Border>

The following example also uses a RotateTransform to rotate a Button 45 degrees clockwise; however, this example sets the RenderTransformOrigin of the button to (0.5, 0.5). As a result, the rotation is applied to the center of the button instead of to the upper-left corner.

The following illustration shows the transformation result for the example that follows.

A 45 degree rotation by using the RenderTransform property with a RenderTransformOrigin of (0.5, 0.5)

A button transformed about its center

<Border Margin="30"   
  HorizontalAlignment="Left" VerticalAlignment="Top"
  BorderBrush="Black" BorderThickness="1">
  <StackPanel Orientation="Vertical">
    <Button Content="A Button" Opacity="1" />
    <Button Content="Rotated Button"
      RenderTransformOrigin="0.5,0.5">
      <Button.RenderTransform>
        <RotateTransform Angle="45" />
      </Button.RenderTransform>
    </Button>
    <Button Content="A Button" Opacity="1" />
  </StackPanel>
</Border>

For more information about transforming FrameworkElement objects, see the Transforms Overview.

See Also

Reference

Transform

Concepts

Transforms Overview

Other Resources

Transformations How-to Topics