WPF如何让控件和TextBlock在拉伸网格的中心对齐
本文关键字:网格 对齐 控件 TextBlock WPF | 更新日期: 2023-09-27 18:17:39
我有以下场景:
<Grid x:Uid="Grid_3" Grid.Row="0" Margin="5" Focusable="False" Visibility="Visible" Background="DarkGray" Opacity="0.4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition x:Uid="RowDefinition_8" Height="Auto"/>
<RowDefinition x:Uid="RowDefinition_9" Height="Auto"/>
</Grid.RowDefinitions>
<controls:LoadingPanel Grid.Row="0" IsLoading="True"
HorizontalLoadingIndicatorAlignment="Center"
VerticalLoadingIndicatorAlignment="Center"
/>
<TextBlock x:Uid="TextBlock_4" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Commit In Process..." />
</Grid>
</Grid>
我想让LoadingPanel
和TextBlock
在Grid
的中心对齐,我已经设置为垂直和水平拉伸。
注意,网格已经在另一个网格中。
所以,我猜你看到的问题是,控件不是垂直集中,而是坐在网格的顶部?
要解决这个问题(同时保持控件彼此低于),只需在控件的两行上下分别添加一个相对大小的行,如下所示:
<Grid x:Uid="Grid_3" Grid.Row="0" Margin="5" Focusable="False" Visibility="Visible" Background="DarkGray" Opacity="0.4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition x:Uid="RowDefinition_8" Height="Auto"/>
<RowDefinition x:Uid="RowDefinition_9" Height="Auto"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<controls:LoadingPanel Grid.Row="1" IsLoading="True"
HorizontalLoadingIndicatorAlignment="Center"
VerticalLoadingIndicatorAlignment="Center"
/>
<TextBlock x:Uid="TextBlock_4" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Commit In Process..." />
</Grid>
这应该能解决问题。
只是为了能够给出这个话题的答案,我重新发布了上面的评论,让@DSF接受它。
显然,这个问题与网格有两行有关,每个控件都在自己的行上。丢弃这些行解决了他的问题。
要修改可见性的顺序,如你所愿,使用Panel。LoadingPanel和Panel上的ZIndex="2"。ZIndex="1"在TextBlock上,像这样