状态栏中的WPF ProgressBar无法拉伸

ao218c7q  于 2023-10-22  发布在  其他
关注(0)|答案(5)|浏览(126)

WPF新手&我有以下XAML

<Window x:Class="Wpf.RossKiosk.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Topmost="True" WindowStartupLocation="CenterScreen" WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None">
<DockPanel LastChildFill="True">

    <StatusBar Name="StatusBarMain" Height="30"  DockPanel.Dock="Bottom">
        <StatusBarItem HorizontalContentAlignment="Left">
            <TextBlock x:Name="TextBlockStatus" Margin="5,0,0,0" />
        </StatusBarItem>
        <StatusBarItem HorizontalContentAlignment="Stretch">
            <ProgressBar x:Name="ProgressBarMain" IsIndeterminate="True" Height="15" />
        </StatusBarItem>
        <StatusBarItem HorizontalContentAlignment="Right">
            <TextBlock x:Name="TextBlockInfo" Margin="5,0,0,0" TextAlignment="Right" />
        </StatusBarItem>
    </StatusBar>

    <Grid Name="GridMain">

        <!-- Dynamically Created buttons -->

    </Grid>

</DockPanel>

我希望ProgressBar填充StatusBar的中心部分,但它只显示了几个像素的宽度。有什么想法吗?

g0czyy6m

g0czyy6m1#

这是由于StatusBar默认使用DockPanel来布局其子节点。请看我的问题和答案在这里。

iszxjhcz

iszxjhcz2#

您将需要使用网格。DockPanel或StackPanel不足以满足您的需求。试试看:

<Grid>
    <StatusBar Name="StatusBarMain" Height="30" HorizontalAlignment="Stretch">
    <StatusBarItem HorizontalContentAlignment="Left">
        <TextBlock x:Name="TextBlockStatus" Margin="5,0,0,0" />
    </StatusBarItem>
    <StatusBarItem HorizontalContentAlignment="Stretch">
        <ProgressBar x:Name="ProgressBarMain" IsIndeterminate="True" Height="15" />
    </StatusBarItem>
    <StatusBarItem HorizontalContentAlignment="Right">
        <TextBlock x:Name="TextBlockInfo" Margin="5,0,0,0" TextAlignment="Right" />
    </StatusBarItem>
   </StatusBar>
    </Grid>
siotufzp

siotufzp3#

这对我有帮助,但我需要添加网格行和列定义,使其正常工作。

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto" ToolTip="abc213">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
ioekq8ef

ioekq8ef4#

我已经为<StatusBarItem>设置了HorizontalContentAlignment ="Stretch",它帮助了:

<StatusBar DockPanel.Dock="Bottom">
        <StatusBarItem DockPanel.Dock="Right" HorizontalContentAlignment="Stretch">
            <ProgressBar IsIndeterminate="True" VerticalAlignment="Bottom" Height="5" Visibility="Visible" Name="CProgressBar" />
        </StatusBarItem>
    </StatusBar>
hxzsmxv2

hxzsmxv25#

这会将进度条拉伸到其容器中

<StatusBarItem  Name="progressBarContainer">
    <ProgressBar Height="{Binding ElementName=progressBarContainer, Path=ActualHeight}" Width="{Binding ElementName=progressBarContainer, Path=ActualWidth}" Value="50" />
</StatusBarItem>

相关问题