2009-02-19 95 views
13

如何让我的状态栏中的TextBlock对齐到右侧?如何让TextBlock右对齐?

我已经告诉它:

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

但文本仍然坐在unobediently左侧。我还有什么要说的?

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

回答

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会为你工作。

+0

非常好,不知道StatusBarItem,谢谢! – 2009-02-19 09:51:28

+0

两个项目不对齐。 只有一个项目对齐正确 – 2012-12-11 08:57:14

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中,它会将您的分隔符减少为点。

1

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