How to do rotation around control's center in XAML

C#WpfXaml

C# Problem Overview


I want do rotate button to 90 degrees but it gets clipped because it rotates arount (0,0). How to make it rotate around center if I don't know it't width in pixels (it's a template for many buttons)

C# Solutions


Solution 1 - C#

You have to set the control's RenderTransformOrigin to 0.5, 0.5.

ex.:

<Button RenderTransformOrigin="0.5, 0.5">
	<RepeatButton.RenderTransform>
	    <RotateTransform Angle="90"/>
	</RepeatButton.RenderTransform>
</RepeatButton>

Solution 2 - C#

<Button ...>
  <Button.LayoutTransform>
    <RotateTransform CenterX="0.5" CenterY="0.5" Angle="90"/>
  </Button.LayoutTransform>
</Button>

Solution 3 - C#

My understanding is that the origin isn't relevant with a LayoutTransform.

MSDN says:

> Setting a transform provides powerful capabilities of scaling and > rotating. However, LayoutTransform ignores TranslateTransform > operations. This is because the layout system behavior for child > elements of a FrameworkElement auto-corrects any offsets to the > position of a scaled or rotated element into the layout and coordinate > system of the parent element.

and the following "correctly" rotates the button.

<Grid ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Grid.Row="1" Grid.Column="1">Excessively Long Button Still Ok
        <Button.LayoutTransform>
            <RotateTransform Angle="90" />
        </Button.LayoutTransform>
    </Button>
</Grid>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPomaView Question on Stackoverflow
Solution 1 - C#Francesco De VittoriView Answer on Stackoverflow
Solution 2 - C#AndyView Answer on Stackoverflow
Solution 3 - C#grantnzView Answer on Stackoverflow