wpf - WPF网格列全宽
问题描述
我很难让我的网格列全宽。如您所见,我在文本块上尝试了 HorizontalAlignment="Stretch"(也尝试了 Center)和 Width="*"(也尝试了 Auto),但似乎都不起作用。
我希望该列是整个窗口宽度,并且“欢迎”文本居中。
<Grid>
<DockPanel LastChildFill="False">
<TextBlock DockPanel.Dock="Top" Text="Drink & Drive"/>
<TextBlock DockPanel.Dock="Bottom" Text="Drink & Drive - 2020"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" Text="WELCOME"/>
</Grid>
</DockPanel>
</Grid>
结果:
谢谢。
解决方案
我建议使用 1 Grid,而不是 Grid + DockPanel + Grid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Drink & Drive"/>
<TextBlock Grid.Row="1" Text="WELCOME" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Row="2" Text="Drink & Drive - 2020"/>
</Grid>
推荐阅读
- node.js - 如何使用 NodeJS 计算 MongoDB 中的总行数?
- firebase - 如何修复 Keycloak 和 Firebase 之间的无效 JWT?
- performance - 使用原始图像的坐标数组构建新图像时如何加快numpy数组迭代
- c# - 当 C# 迭代 zipfile 时,HttpPostedFileBase 将内容长度变为 0
- magento2 - Magento 2 - 管理订单网格,日期过滤器不过滤正确的订单
- c# - 如何从 TwinCat 读取包含结构数组的结构到 C#
- ruby-on-rails - Rails 从一个表单创建多行
- c++ - 如何让 CMake find_library 为构建类型找到正确的库
- java - 如何在android java中的imageview上绘制多个点
- drupal - 如何获取drupal视图更改钩子中的总行数?