WPF网格水平对齐不起作用。尺寸不变

dev*_*rgg 5 .net c# wpf user-interface xaml

我有一个堆栈,堆栈中有一个网格。

调整窗口大小时,需要增加堆栈和网格的大小。我将Stack and Grid Horizo​​ntalAlignment设置为“ Stretch”

堆栈工作正常,但是当我调整窗口大小时网格尺寸不会增加

这是我的代码。

<StackPanel Background="Blue" Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Column="1" Margin="13,0,0,0" >
   <Grid HorizontalAlignment="Stretch" Background="Beige" Width="515">

        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition Height="240" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <Image x:Name="imageMap" Source="Resources/images.jpg" HorizontalAlignment="Stretch" Grid.Row="0" Stretch="Fill" Margin="0,0,0,0" />

   </Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

请指教

小智 4

即使您从网格中删除了宽度,它也不会起作用,因为 StackPanel 的方向设置为水平。当这样设置时,StackPanel 会为其子级提供其在测量布局过程中所需的宽度量(请参阅此处的布局)

要解决此问题,您需要将方向更改为垂直,或使用不同的容器,例如 DockPanel