我在主窗口中有以下 Grid
:
<Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto">
</Grid>
我正在网格中动态添加一个 UserControl
。主窗口只有这个网格。我希望网格在水平和垂直方向上都被拉伸(stretch),并且应该填满整个窗口。网格水平拉伸(stretch)但不垂直拉伸(stretch)。知道为什么吗?我需要做什么才能使网格垂直拉伸(stretch)?
编辑
这是完整的 XAML。
<Window x:Class="Sensitech.TurboValidator.UserControls.ConveyorBelt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ConveyorBelt" Height="329" Width="714" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto" Background="Cyan">
</Grid>
</Window>
最佳答案
尝试将行高设置为*
<Grid Name="gridMain" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
同样定义ColumnDefinition,设置Width为*
还要确保网格是不拉伸(stretch)的而不是用户控件。您可以通过为 Grid 分配一些背景颜色来轻松做到这一点。
关于c# - WPF 网格不垂直拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8641456/