WPF状态栏,拉伸文本块以占用尽可能多的空间

时间:2012-12-14 13:14:26

标签: wpf xaml statusbar

我使用3个TextBlocks创建了自己的简单statusbarcontrol。现在我希望第一个Textblock占用尽可能多的空间。我似乎没有完成。现在它只需要显示文本所需的空间。

XAML:

  <StatusBar Background="{StaticResource GradientBrush}">
<StatusBar.ItemsPanel>
  <ItemsPanelTemplate>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
      </Grid.ColumnDefinitions>
    </Grid>
  </ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem HorizontalAlignment="Left"
               HorizontalContentAlignment="Right">
  <Border BorderThickness="1"
          BorderBrush="Black"
          Padding="5 0 5 0"
          Background="White">
    <TextBlock Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             Foreground="{Binding TextColorMessage}"
             Background="White"
             />
  </Border>
</StatusBarItem>
<Separator Grid.Column="1" />
<StatusBarItem Grid.Column="2"
               HorizontalAlignment="Right">
  <TextBlock Text="{Binding Path=DatabaseName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StatusBarItem>
<Separator Grid.Column="3" />
<StatusBarItem Grid.Column="4"
               HorizontalAlignment="Right">
  <TextBlock Text="{Binding Path=ComputerName}" />
</StatusBarItem>

My StatusBar

4 个答案:

答案 0 :(得分:8)

这很简单: 你将StatusBarItem设置为Horizo​​ntalAlignment =“Left”,它应该是“strech”,对于Horizo​​ntalContentAlignment也是如此。 还建议在边界上设置margin = 0。 这就是我所做的,所以它对我有用:

<StatusBarItem HorizontalAlignment="Stretch"
               HorizontalContentAlignment="Stretch">
    <Border BorderThickness="1"
            BorderBrush="Black"
            Margin="0"
            Padding="5 0 5 0"
            Background="White">
        <TextBlock Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                   Foreground="{Binding TextColorMessage}"
                   Background="White"/>
    </Border>
</StatusBarItem>

如果这有用,请标记为答案

答案 1 :(得分:5)

尝试将StatusBarItem的HorizontalAlignmentHorizontalContentAlignment设置为Stretch

答案 2 :(得分:0)

将StatusBarItem的Horizo​​ntalContentAlignment设置为Stretch,将TextBlock的TextAlignment属性设置为Right。 (第二个设置仅适用于第一个StatusBarItem,因为您似乎尝试将文本对齐在其中。)

答案 3 :(得分:0)

StatusBar只有一个Horizo​​ntalAlignment = Right项目有效,因此您只能获取所需位置的最后一项。您可以设置statusBar的FlowDirection =&#34; RightToLeft&#34;并按相反的顺序添加项目。详细信息:正确放置的项目不需要包围StatusBarItem,但左边的项目需要拉伸。以下代码:

<StatusBar VerticalAlignment="Bottom" FlowDirection="RightToLeft"><!--flow right to left cause items right aligned-->
    <!--make item's flow back left to right, avoid display disorder-->
    <TextBlock x:Name="textBlock_status_R1" Text="111.147.168.20" Grid.Column="2" Margin="10,0" FlowDirection="LeftToRight"/>
    <Separator/>
    <TextBlock x:Name="textBlock_status_R2" Text="Working" Margin="10,0" FlowDirection="LeftToRight"/>
    <Separator/>
    <!--To fill rest space,need StatusBarItem packing and default HorizontalAlignment="Stretch"-->
    <StatusBarItem FlowDirection="LeftToRight">
        <TextBlock x:Name="textBlock_status_L1" Text="Information here."/>
    </StatusBarItem>
</StatusBar>