为什么 WPF 进度条不会拉伸以适应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13435699/
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:15:57  来源:igfitidea点击:

Why won't the WPF progressbar stretch to fit?

wpfalignmentstackpanel

提问by John NoCookies

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:

进度条非常小,可能有 2 或 3 个像素宽,然后是文本块和空白区域。所以我尝试明确地将元素停靠在两侧:

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

徒劳无功。我还尝试通过HorizontalAlignment="Stretch"在进度条上进行设置来修改每个解决方案,但没有任何变化。我如何拉伸它以填充文本块渲染后的所有空间?

采纳答案by Adi Lester

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

除去DockPanel.Dock="Left"ProgressBar和切换控制的顺序:

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

By default, DockPanelhas its property LastChildFillset to true, which will make the ProgressBartake the available space.

默认情况下,DockPanel将其属性LastChildFill设置为true,这将ProgressBar占用可用空间。

回答by user1834059

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/

啊我明白你在做什么。这可能是使用具有 2 列定义的网格的更好地方。第一个(左边的)columndefinition 与 Width="*" 和第二个(右边的)的宽度设置为 Width="Auto"。有关 auto 与 * 的更多信息,请参阅http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9a7e6591-1fae-4295-b68a-be97e8e53d06/