tags:

views:

127

answers:

1

I am working on a C# Silverlight application that needs some text rotated 90 degrees CCW and for it to be center within an image that is 100px x 221px. The rotation part was easy to do in the design view, but centering the text has been a nightmare. (I am using a 16px font that cant be changed or resized)

My text is dynamic and can be one or two lines. When it is two lines long I can center it fine...but if It is only one line long, I cannot center it. The only way to center it so far is to resize the box and move it to the right.

Is there an easy way of doing this?

alt text

A: 

The answer turned out to be simple:

<Grid x:Name="LayoutRoot">
    <Image x:Name="Background" Source="Background.png" Stretch="Fill"/>
    <TextBlock x:Name="Title"
        Margin="-19.75,68.25,-21.25,67.806" Text="Here is some text to fill this up"
        Foreground="#FF00A33D"
        FontSize="22" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto"
        TextAlignment="Center" TextWrapping="Wrap"
    >
        <TextBlock.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="-90"/>
                <TranslateTransform/>
            </TransformGroup>
        </TextBlock.RenderTransform>
    </TextBlock>
</Grid>

Then in the cs file set the alignment when you change the text

Title.VerticalAlignment = VerticalAlignment.Bottom;
Mitchell Skurnik