2010-03-18 117 views
1

我正在研究一種C#Silverlight應用程序,它需要一些文本將CCW旋轉90度,並使其在100px x 221px的圖像中居中。旋轉部分在設計視圖中很容易做到,但將文本居中成爲一場噩夢。 (我正在使用不能更改或調整大小的16px字體)在Silverlight中水平和垂直居中文本

我的文本是動態的,可以是一行或兩行。當它是兩條線時,我可以將它居中......但如果它只有一條線,我不能居中。到目前爲止,將它居中的唯一方法是調整框的大小並將其移到右側。

有沒有簡單的方法來做到這一點?

alt text http://www.freeimagehosting.net/uploads/f0435a8c65.png

+0

想發佈一些XAML?如果沒有看到您目前正在嘗試做什麼,很難診斷您需要做什麼...... – JerKimball 2010-03-18 21:14:11

+0

謝謝您的轉載。我能夠自己想出來。請參閱下面的答案。 – 2010-03-18 22:24:53

回答

0

答案竟然是簡單的:

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

然後在CS文件中設置對齊當您更改文本

Title.VerticalAlignment = VerticalAlignment.Bottom;