如何摆脱保证金定位

本文关键字:定位 保证金 何摆脱 | 更新日期: 2023-09-27 18:00:34

我只是有一个关于wpf中元素缩放/定位问题的一般性问题。因为我是wpf的新手,所以我只是在应用程序的视觉预览中拖动并重新缩放元素。当然,这已经为每个元素设置了边距,现在当我调整主窗口的大小或在全屏模式下运行应用程序时,一切都会变得混乱和重叠。我现在明白了边际的作用,它是非常静态的。将其更改为动态定位(网格行/列)的最佳方法是什么。我只是现在不知道。

如何摆脱保证金定位

为了在网格的第二行和第二列中放置按钮,可以使用以下XAML代码。

在该示例中值得注意的是,*-定义相对于其他*-大小,因此1*2*大小的一半,依此类推

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="2*" /> <!-- Make this row double in height -->
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" /> <!-- You don't need a number either -->
        <ColumnDefinition Width="*" /> <!-- All columns are the same size -->
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Grid.Row="1" Grid.Column="1" Content="Hello" />
</Grid>

您应该使用网格

在网格中,您可以定义行的高度、列的宽度。。。看起来像这个

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="5*" />
    </Grid.RowDefinitions>
</Grid>

您应该看看这篇文章