如何让TextBlock右对齐?

时间:2009-02-19 09:06:12

标签: xaml

如何在下面的状态栏中找到TextBlock以对齐?

我告诉过:

  • 的Horizo​​ntalAlignment = “右”
  • TextAlignment = “右”

但是文字仍然不守规矩地坐在左边。还有什么我要说的?

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

3 个答案:

答案 0 :(得分:16)

我玩过你的代码,并通过使用StatusBarItem而不是TextBlock设法让它看起来“正确”(没有双关语):

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

不确定TextBlock发生了什么 - 我的经验表明,Horizo​​ntalContentAlignment和Horizo​​ntalAlignment(在StatusBar和TextBlock上)的某些组合应该可以达到你想要的效果。无论如何 - 希望StatusBarItem适合你。

答案 1 :(得分: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>

此示例不会弄乱您的分隔符。基于取自http://kent-boogaart.com/blog/the-perfect-wpf-statusbar

的示例

您不应将分隔符放在StatusBarItem中,它会将分隔符缩小为点。

答案 2 :(得分:1)

对于正在寻找标题中问题答案的人(不一定是在状态栏中使用),我发现Label比TextBlock更好,因为它可以控制对齐并且仍然感觉语义正确。