Search code examples
wpfxamlstatusbar

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


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.

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


Solution

  • You had set the StatusBarItem HorizontalAlignment="Left" when it should be Stretch, same for the HorizontalContentAlignment. Also would suggest setting Margin="0" on the border.

    This is what I did so it will work for me:

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