【问题标题】:Simple grid layout question简单的网格布局问题
【发布时间】:2010-04-01 04:10:55
【问题描述】:

我不敢相信在使用 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>

【问题讨论】:

    标签: wpf vb.net xaml grid


    【解决方案1】:
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    

    来自GridUnitType Enumeration

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

    【讨论】:

    • 谢谢!事实证明,我在状态栏中有一个图像设置为“隐藏”,它的默认设置是填充它的可用区域。这就是为什么我的底行一直占据屏幕 90% 的原因! :) 一旦我解决了这个问题,你的解决方案就完美了。
    【解决方案2】:

    你使用:

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

    Auto 将根据内容调整大小,而 * 将填充空间。如果您之间有多个“内容”区域,您也可以使用多个:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 2011-01-05
      • 2019-10-11
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多