WPF中的网格扩展

时间:2009-09-03 15:52:34

标签: wpf grid

亥 目前我有这样的网格。

                                                                                                    

    <Grid  Name="tGrid1"  Grid.Row="0" Background="Black" >
    </Grid>



    <Grid Name="Grid2"  Grid.Row="1"  Background="LightBlue" >
    </Grid>



    <Grid Name="Grid3"  Grid.Row="2" Background="LightGray" Height="auto" >

    </Grid>




    <Grid Name="Grid4"   Grid.Row="3"   Background="LightGreen" >
    <Button HorizontalAlignment="Left" IsEnabled="True" Margin="13.5,5,0,5" Name="TestBtn" Width="50" Click="test_Click" >Test</Button>
    </Grid>

</Grid>

我想要的是,当我单击测试按钮时,网格1和网格2必须折叠,并且空间必须占用网格3.这意味着外网格的总高度超出500,网格3应该占用height = 465,Grid 4应该是ocupy Height = 35。 我怎样才能做到这一点?我该如何更改Grid的高度定义? Thanx提前 冠者

2 个答案:

答案 0 :(得分:0)

不确定这是否是您所要求的,但在test_Click方法中您可以使用此功能:

  private void test_Click(object sender, RoutedEventArgs e)
  {
     tGrid1.Width = 0;
     tGrid1.Height = 0;
     Grid2.Width = 0;
     Grid2.Height = 0;

     Grid3.Height = 465;
     Grid4.Height = 35;
  }

如果“崩溃”是指“隐藏”,则可以将heightwidth tGrid1Grid2设置为0来实现。

答案 1 :(得分:0)

亥 我找到了解决方案。

  <DockPanel  >
 <Grid  Name="tGrid1"  Grid.Row="0" DockPanel.dock="Top" Background="Black" >
 </Grid>

 <Grid Name="Grid2"  Grid.Row="1"  DockPanel.dock="Top" Background="LightBlue" >
 </Grid>

<Grid Name="Grid4"   Grid.Row="3"  DockPanel.dock="Bottom"  Background="LightGreen" >
<Button HorizontalAlignment="Left" IsEnabled="True" Margin="13.5,5,0,5" Name="TestBtn" Width="50" Click="test_Click" >Test</Button>
 </Grid>

<Grid Name="Grid3"  Grid.Row="2" DockPanel.dock="Top" Background="LightGray" Height="auto" >

</Grid>
</DockPanel>




 private void test_Click(object sender, RoutedEventArgs e)
  {
     tGrid1.visibility=visibility.collapsed;
Grid2.visibility=Visibility.collapsed;
  }

这很好用。 谢谢 冠者