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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13879397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:37:31  来源:igfitidea点击:

WPF Statusbar, stretch textblock to take as much space as possible

wpfxamlstatusbar

提问by PitAttack76

I created my own simple statusbarcontrol with 3 TextBlocks. Now I would like that the first Textblock takes as much space as it has available. That I don't seem to get done.. Now it only takes the space needed to display the text.

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

XAML:

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

我的状态栏

回答by Eyal H

Well this is pretty straight forward: you had set the StatusBarItem HorizontalAlignment="Left" when it should be "strech", same for the HorizontalContentAlignment. Also would suggest setting margin=0 on the border. this is what i did so it will work for me:

嗯,这很简单:当它应该是“strech”时,您已经设置了 StatusBarItem Horizo​​ntalAlignment="Left",对于 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>

if this is helpful please mark as answer

如果这有帮助,请标记为答案

回答by Florian Gl

Try setting the StatusBarItem's HorizontalAlignmentand HorizontalContentAlignmentto Stretch.

尝试将 StatusBarItem 的HorizontalAlignment和设置HorizontalContentAlignmentStretch

回答by qqbenq

Set the StatusBarItem's HorizontalContentAlignment to Stretch and the TextBlock's TextAlignment property to Right. (This second setting is only needed for the first StatusBarItem as it seems that you try to align the text to the right in it.)

将 StatusBarItem 的 Horizo​​ntalContentAlignment 设置为 Stretch,将 TextBlock 的 TextAlignment 属性设置为 Right。(仅第一个 StatusBarItem 需要第二个设置,因为您似乎试图将文本与其中的右侧对齐。)

回答by Gao.he

StatusBar has only one HorizontalAlignment=Right item to effect, so you can get only the last item in wanted place. You can set statusBar's FlowDirection="RightToLeft" and add item in reversed order. Details: right placed items needn't StatusBarItem surrounded, but the left one need to be streched. Following code:

StatusBar 只有一个 Horizo​​ntalAlignment=Right 项生效,因此您只能获得所需位置的最后一项。您可以设置 statusBar 的 FlowDirection="RightToLeft" 并以相反的顺序添加项目。详细说明:右侧放置的项目不需要 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>