简单的网格布局问题

时间:2010-03-31 20:06:47

标签: wpf vb.net xaml grid

我无法相信在与WPF合作3个月后我又回到了这一点:)

考虑非常常见的设置:

如何配置行高,使顶行和底行(菜单栏和状态栏)大小适合其内容的高度,中间行(主要内容)填充程序中剩余的可用空间?

我无法修正顶部/底部行的高度,因为其内容的高度可能会有所不同。

<Window>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
       <RowDefinition/>
    </Grid.RowDefinitions>

  <Menu Grid.Row=0>
  ...menuitems
  </Menu>

      <Grid Grid.Row=1>
       ...main application content here (like most programs)
      </Grid>

      <StatusBar>
      ...statusbaritems
      </StatusBar>
   </Grid>
</Window>

2 个答案:

答案 0 :(得分:4)

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

来自GridUnitType Enumeration

  

自动:大小由内容对象的大小属性决定   星号:该值表示为可用空间的加权比例。

答案 1 :(得分:3)

您使用:

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

自动会调整内容大小,*会填充空间。如果您之间有多个“内容”区域,也可以使用倍数:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="2*" /> <!-- Will be 2/3rd of main space -->
    <RowDefinition Height="1*" /> <!-- Will be 1/3rd of main space -->
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>