Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a TextBlock to right-align?

Tags:

xaml

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>
like image 486
Edward Tanguay Avatar asked Feb 19 '09 09:02

Edward Tanguay


2 Answers

I've had a play with your code and managed to make it look "right" (no pun intended) by using a StatusBarItem rather than a TextBlock:

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

Not sure what's happening with the TextBlock - all my experience says that some combination of HorizontalContentAlignment and HorizontalAlignment (on both the StatusBar and the TextBlock) should achieve what you want. Anyway - hopefully the StatusBarItem will work for you.

like image 89
Matt Hamilton Avatar answered Sep 25 '22 10:09

Matt Hamilton


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

like image 27
invalidusername Avatar answered Sep 24 '22 10:09

invalidusername