0

Grid在 XAML 中有这样的定义:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition />
        <RowDefinition Height="30" />
        <RowDefinition Height="2*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="4*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="2*" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="5" />
</Grid>

如果您想通过添加/删除行/列来修改网格,您可能需要更新网格的许多子项的Grid.Row/Grid.Column附加属性(例如,删除前两行时,StackPanel需要从 更改Grid.Row="5"Grid.Row="3")。

如果网格已经有很多孩子,这可能会变得很麻烦。因此,在设计时修改已填充的网格时,我使用的是 XAML 设计器的可视化端,因为它会相应地自动更新网格内容的附加属性。

对于添加,这非常有效:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition />
        <RowDefinition Height="30" />
        <RowDefinition Height="80" /><!-- inserted, simply adjust height as needed -->
        <RowDefinition Height="2*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="4*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="2*" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="6" /><!-- updated -->
</Grid>

但是,对于删除,它还​​会更新任何具有动态大小定义(* 单位)的 / RowDefinitionColumnDefinition例如,在删除前两行后,我剩下的是这样的:

<Grid>
    <Grid.RowDefinitions>
        <!-- first two removed -->
        <RowDefinition Height="30" />
        <RowDefinition Height="59*" /><!-- was 2* -->
        <RowDefinition Height="30" />
        <RowDefinition Height="117*" /><!-- was 4* -->
        <RowDefinition Height="30" />
        <RowDefinition Height="59*" /><!-- was 2* -->
        <RowDefinition Height="29*" /><!-- was implicit 1* -->
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="3" /><!-- updated -->
</Grid>

理想情况下,我想得到以下结果:

<Grid>
    <Grid.RowDefinitions>
        <!-- first two removed, others unchanged -->
        <RowDefinition Height="30" />
        <RowDefinition Height="2*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="4*" />
        <RowDefinition Height="30" />
        <RowDefinition Height="2*" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="3" /><!-- updated -->
</Grid>

现在,我总是要记住在删除可视化设计器中的行或列之前复制我想要保留的定义,这样我就可以将复制的定义粘贴到创建的混乱中。但这甚至都行不通,除非你只是在边缘删除。

是否有一个选项可以告诉 XAML 设计器不要对大小定义进行伪优化?

我尝试了诸如在单击“删除行”菜单项时按住 Shift 或 Control 之类的方法,并且我查看了 VS 提供的(非常少的)XAML Designer 设置,但到目前为止无济于事。

我有 Visual Studio 2019 专业版。

4

0 回答 0