Alex Rivera | Logout

How to get a TextBlock to right-align?

Asked 2009-02-19T09:06:11.823
14

How do I get the TextBlock in my status bar below to align to the right?

I've told it to:

  • HorizontalAlignment="Right"
  • TextAlignment="Right"

but the text is still sitting unobediently on the left. What else do I have to say?

<Window x:Class="TestEvents124.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300"
        MaxWidth="700" Width="700"
        >
    <DockPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto">

        <StatusBar Width="Auto" Height="25" Background="#888" DockPanel.Dock="Bottom" HorizontalAlignment="Stretch">
            <TextBlock 
                Width="Auto" 
                Height="Auto" 
                Foreground="#fff" 
                Text="This is the footer." 
                HorizontalAlignment="Right"
                TextAlignment="Right"
                />
        </StatusBar>

        <GroupBox DockPanel.Dock="Top" Height="Auto" Header="Main Content">
            <WrapPanel Width="Auto" Height="Auto">
                <TextBlock Width="Auto" Height="Auto" TextWrapping="Wrap" Padding="10">
                This is an example of the content, it will be swapped out here.
                </TextBlock>
            </WrapPanel>
        </GroupBox>

    </DockPanel>

</Window>
Edit
Report

1 Answer

3
<StatusBar>
    <StatusBar.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </StatusBar.ItemsPanel>
    <StatusBarItem Grid.Column="0">
        <TextBlock>something</TextBlock>
    </StatusBarItem>
    <Separator Grid.Column="1" />
    <StatusBarItem Grid.Column="2">
        <TextBlock>logged in</TextBlock>
    </StatusBarItem>
</StatusBar>

This example won't mess up your Separator. Based on an example taken from http://kent-boogaart.com/blog/the-perfect-wpf-statusbar

You shouldn't put a Separator in a StatusBarItem, it will reduce your separator to a dot.

answered 2010-04-25T16:32:47.503

Your Answer