这是我的原始代码:

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

进度栏很小,可能只有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>

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

最佳答案

DockPanel.Dock="Left"中删除ProgressBar并切换控件的顺序:

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

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

关于wpf - 为什么WPF进度栏无法适应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13435699/

10-13 06:55