locked
Center-Align Text Within A Label RRS feed

  • Question

  • How do you center-align text within a label? I was told before how to center the content in a label in order to get it to always appear in the center of the label dimensions, but I also need to center-align the text as well. Right now my text does appear in the center of the label, but it's still left-justified once it's there.
    Monday, May 17, 2010 9:42 PM

All replies

  • Can you post the code thats not working?  Is this WPF or Silverlight, what version?

    Either of these work for me in SL4

    <sdk:Label HorizontalAlignment="Center" VerticalAlignment="Top" Width="161"/>

    or

    <sdk:Label HorizontalAlignment="Center" VerticalAlignment="Top" Width="161">
                <TextBlock Text="some text"></TextBlock>
            </sdk:Label>

    Monday, May 17, 2010 10:11 PM
  • It's a WPF, and I'm using Blend 2.

    Here's the code, if it helps:

     

    <Style x:Key="TextStyle1" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FFFFFFFF"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontFamily" Value="./#Arial"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>

     

    When I used a text box or text block, the text alignment options are made available to me. But when I use a label, the text align options are disabled in the Properties panel.

    Monday, May 17, 2010 10:22 PM
  • Yes those properties don't exist for label only the standard alignment under layout and the content alignment when you expand additional options in layout panel.

    Strange. I don't have blend 2 anymore, but your code works in Blend 3 and 4 for me. I just had to assign a foreground color to the label so I could so the text on the default white background.

    Tuesday, May 18, 2010 2:54 PM