开发者

Center text horizontally and vertically in Silverlight

开发者 https://www.devze.com 2022-12-24 11:34 出处:网络
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 vi

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 http://www.freeimagehosting.net/uploads/f0435a8c65.png


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号