Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't the WPF progressbar stretch to fit?

This is my original code:

<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
        <ProgressBar Height="23" Name="searchProgressBar" Foreground="Blue" BorderBrush="#00000000" BorderThickness="1" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
        <TextBlock Text="asdf" Height="23"  Name="progressTextBlock" VerticalAlignment="Top" Foreground="Red" HorizontalAlignment="Right"/>
</StackPanel>

The progressbar was very small, maybe 2 or 3 pixels wide, then there was the text block and empty space after. So I tried explicitly docking the elements to sides:

<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" >
            <ProgressBar DockPanel.Dock="Left" Height="23" Name="searchProgressBar" Foreground="Blue" BorderBrush="#00000000" BorderThickness="1" VerticalAlignment="Top" />
            <TextBlock DockPanel.Dock="Right" Text="asdf" Height="23"  Name="progressTextBlock" VerticalAlignment="Top" Foreground="Red" HorizontalAlignment="Right"/>
</DockPanel>

No avail. I also tried modifying each solution by setting HorizontalAlignment="Stretch" on the progress bar, but there's no change. How do i stretch it to fill all the space there is after the text block has been rendered?

like image 873
John NoCookies Avatar asked Nov 17 '12 23:11

John NoCookies


2 Answers

Remove DockPanel.Dock="Left" from the ProgressBar and switch the order of the controls:

<DockPanel>
    <TextBlock DockPanel.Dock="Right" Height="23"  VerticalAlignment="Top" HorizontalAlignment="Right"/>
    <ProgressBar Height="23" VerticalAlignment="Top" />
</DockPanel>

By default, DockPanel has its property LastChildFill set to true, which will make the ProgressBar take the available space.

like image 179
Adi Lester Avatar answered Oct 05 '22 20:10

Adi Lester


ahh I see what you're trying to do. This is probably a better place to use a grid with 2 column definitions. The first(the left one) columndefinition with Width="*" and the second(the right one) with a width set to Width="Auto". For more info about auto vs * see http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9a7e6591-1fae-4295-b68a-be97e8e53d06/

like image 20
user1834059 Avatar answered Oct 05 '22 19:10

user1834059