代码背后的网格边距

时间:2014-09-24 08:24:09

标签: c# wpf grid margin

我有一个wpf应用程序,其中两个网格在一个单独的网格中,如此

<window ....>
  <grid x:name="main_grid">
    <grid x:name="panel1">
        //...some stuff//
    </grid>
    <grid x:name="panel2">
        //...some stuff//
    </grid>
  </grid>
</window>

main_grid的位置适合整个窗口 我想从后面的代码中将Panel1和Panel2边距向左和向上定位为0.

我尝试的是这样的,

private void window_Loaded(object sender, RoutedEventArgs e)
{
   int left = Convert.ToInt16(main_grid.Margin.Left);
   int top = Convert.ToInt16(main_grid.Margin.Top);
   panel1.margin = new Thickness(left, top, 0, 0);
   panel2.margin = new Thickness(left, top, 0, 0);
}

然而,它不起作用。数据... 有人有任何解决方案吗?请...

1 个答案:

答案 0 :(得分:2)

为什么不在XAML中这样:

<Grid x:name="main_grid"
      Margin="10,20,30,40">
    <Grid x:name="panel1"
          Margin="10,20,30,40">
    </Grid>
    <Grid x:name="panel2"
          Margin="10,20,30,40">
    </Grid>
</Grid>