Partilhar via


Como: 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.

Exemplo

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. A RotateTransform é aplicada à propriedade RenderTransform.

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

A 45 degree clockwise rotation by using the RenderTransform property

Um botão transformado com 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)

Um botão invertido transformado ao redor de seu centro

<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 Visão Geral sobre Transformações.

Consulte também

Conceitos

Visão Geral sobre Transformações

Referência

Transform

Outros recursos

Transformations How-to Topics