代码之家  ›  专栏  ›  技术社区  ›  Scott Marlowe

如何将StackPanel分配给代码中的特定网格行/列(而不是XAML)?

  •  1
  • Scott Marlowe  · 技术社区  · 14 年前

    鉴于此XAML:

    <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Margin="5">
    

    如何在(c/vb)代码中分配grid.row和grid.column属性?

    StackPanel stackPanel = new StackPanel();
    stackPanel.???
    
    2 回复  |  直到 14 年前
        1
  •  9
  •   Anthony    14 年前

    我想这就是你想要的:

        MyStackPanel.SetValue(Grid.RowProperty, 1);
        MyStackPanel.SetValue(Grid.ColumnProperty, 2);
    

    希望这有帮助

        2
  •  6
  •   Richard McGuire    14 年前

    您应该能够执行以下操作,其中“mygrid”是网格控件的名称。

    StackPanel stackPanel = new StackPanel();
    
    Grid.SetColumn(stackPanel, 0);
    Grid.SetRow(stackPanel, 1);
    
    myGrid.Children.Add(stackPanel);