2009-02-19 94 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更好,因爲它可以控制對齊並仍然感覺語義正確。