【问题标题】:Why won't the WPF progressbar stretch to fit?为什么 WPF 进度条不能拉伸以适应?
【发布时间】:2012-11-06 07:21:13
【问题描述】:

这是我的原始代码:

<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" 来修改每个解决方案,但没有任何变化。如何拉伸它以填充文本块渲染后的所有空间?

【问题讨论】:

    标签: wpf alignment stackpanel


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-20
        • 2013-08-22
        • 1970-01-01
        相关资源
        最近更新 更多