none
TextBlock Alignment

    Question

  • Code Snippet

    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontFamily="Segoe UI">

    <Page.Resources>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Button}">
            <Border Background="#FFF0F0F0" VerticalAlignment="Center" HorizontalAlignment="Center">
              <StackPanel Orientation="Horizontal">
                <Image Width="15"/>
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Width="60" Background="#FF707070" Text="{Binding RelativeSource={RelativeSource Self}, Path=TemplatedParent.Content}"/>
                <TextBlock FlowDirection="RightToLeft" Width="15" FontSize="8" VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource Self}, Path=TemplatedParent.Tag}"/>
              </StackPanel>
           </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    </Style>

    </Page.Resources>

     

     

    <Grid Background="#FF000040">

      <Button Content="Click me!" Tag="C" VerticalAlignment="Top"/>
    </Grid>
    </Page>

     

     


     

    Can anybony tell why 'Click me!' isn't centered?

    Thursday, July 26, 2007 8:44 AM

Answers

  • Hi,

    You've defined the HorizontalAlignment property property for the TextBlock, but this property defines the horizontal alignment of the TextBlock, not of its content. You should set the TextAlignment property of the TextBlock to Center and then you get what you want.

    Best regards,

    Benny
    Thursday, July 26, 2007 9:10 AM

All replies

  • Hi,

    You've defined the HorizontalAlignment property property for the TextBlock, but this property defines the horizontal alignment of the TextBlock, not of its content. You should set the TextAlignment property of the TextBlock to Center and then you get what you want.

    Best regards,

    Benny
    Thursday, July 26, 2007 9:10 AM
  • Silly me.

     

    Thanks.

     

    Thursday, July 26, 2007 9:59 AM