Search code examples
wpfxamltextblock

How to horizontally align textblocks within a stackpanel


I have a group of textblocks that are located within a stackpanel. There is no options to horizontally align the content of the textblocks. How do I horizontally align these columns to 'center' without hacking the margins?

enter image description here

Here is the xaml:

<StackPanel Orientation="Horizontal" Background="{Binding RowColor}" >
    <TextBlock Text="{Binding PlayerNumber}" Padding="5" Width="50" />
    <TextBlock Text="{Binding PlayerName}"  Padding="5"  Width="200" />
    <TextBlock Text="{Binding Points}"  Padding="5"  Width="50"/>
    <TextBlock Text="{Binding Fouls}"  Padding="5" Width="50" />
    <TextBlock Text="{Binding Rebounds}"  Padding="5"  Width="50" />
</StackPanel>

Solution

  • Have you tried TextAlignment?

    Something like:

    <StackPanel Orientation="Horizontal" Background="{Binding RowColor}" >
       <TextBlock Text="{Binding PlayerNumber}" Padding="5" Width="50" TextAlignment="Center" />
       <TextBlock Text="{Binding PlayerName}"  Padding="5"  Width="200" TextAlignment="Center" />
       <TextBlock Text="{Binding Points}"  Padding="5"  Width="50" TextAlignment="Center"/>
       <TextBlock Text="{Binding Fouls}"  Padding="5" Width="50" TextAlignment="Center" />
       <TextBlock Text="{Binding Rebounds}"  Padding="5"  Width="50" TextAlignment="Center" />
    </StackPanel>