WPF 进度条不会正确左对齐
WPF Progressbar won't left align properly
我有以下 XAML 代码在我的 window 中显示一组控件:
<Grid Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Width="80" />
<TextBlock Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}"
Foreground="#B0B0B0" VerticalAlignment="Top" />
<TextBlock Grid.Column="1" Grid.Row="1" Margin="5,0" Text="{Binding Description}" Foreground="#c9c9c9"
VerticalAlignment="Top" />
<ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" />
</Grid>
它基本上是左侧的图像和右侧的两个 TextBlocks 和 ProgressBar。
我的问题是,当我的 window 变宽时,最后的 ProgressBar 会无限延伸。
我希望我的进度条在某个点停止增长。因此,如果用户拉伸 window,它将在给定的 MaxWidth 处停止。
所以我尝试了这个:
<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200"/>
但是如果 ProgressBar 被拉伸超过 MaxWidth,这将使 ProgressBar 在 window 的中间居中,从而在左侧的图像和 ProgressBar 之间生成 space。
我希望进度条保持在左侧,但 HorizontalAlignment="Left"
将进度条最小化到几个像素:
<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" HorizontalAlignment="Left"/>
有谁知道如何进行这项工作吗?
您可以尝试绑定到其他元素的宽度:
<TextBlock x:Name="title" Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}" Foreground="#B0B0B0" VerticalAlignment="Top" />
<ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" Width="{Binding Path=ActualWidth, ElementName=title}" MaxWidth="200" HorizontalAlignment="Left"/>
您还可以将 ProgressBar
包装在 Grid
中,其中两列定义如下:
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="200"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ProgressBar Grid.Column="0" Value="50" Margin="5" Height="15"/>
</Grid>
我有以下 XAML 代码在我的 window 中显示一组控件:
<Grid Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Width="80" />
<TextBlock Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}"
Foreground="#B0B0B0" VerticalAlignment="Top" />
<TextBlock Grid.Column="1" Grid.Row="1" Margin="5,0" Text="{Binding Description}" Foreground="#c9c9c9"
VerticalAlignment="Top" />
<ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" />
</Grid>
它基本上是左侧的图像和右侧的两个 TextBlocks 和 ProgressBar。
我的问题是,当我的 window 变宽时,最后的 ProgressBar 会无限延伸。 我希望我的进度条在某个点停止增长。因此,如果用户拉伸 window,它将在给定的 MaxWidth 处停止。 所以我尝试了这个:
<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200"/>
但是如果 ProgressBar 被拉伸超过 MaxWidth,这将使 ProgressBar 在 window 的中间居中,从而在左侧的图像和 ProgressBar 之间生成 space。
我希望进度条保持在左侧,但 HorizontalAlignment="Left"
将进度条最小化到几个像素:
<ProgressBar Grid.Row="2" Value="50" Margin="5" Height="15" MaxWidth="200" HorizontalAlignment="Left"/>
有谁知道如何进行这项工作吗?
您可以尝试绑定到其他元素的宽度:
<TextBlock x:Name="title" Grid.Column="1" Grid.Row="0" Margin="5" FontWeight="Bold" Text="{Binding Titel}" Foreground="#B0B0B0" VerticalAlignment="Top" />
<ProgressBar Grid.Column="1" Grid.Row="2" Value="50" Margin="5" Height="15" Width="{Binding Path=ActualWidth, ElementName=title}" MaxWidth="200" HorizontalAlignment="Left"/>
您还可以将 ProgressBar
包装在 Grid
中,其中两列定义如下:
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="200"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ProgressBar Grid.Column="0" Value="50" Margin="5" Height="15"/>
</Grid>