2012-01-31 68 views
0

在这个简单的示例中,我有5 GroupBox es,直接在Grid下。我尝试声明1列和5行,以便它们垂直堆叠。我没有使用StackPanel,因为我希望能够稍后智能地设置这些组合框的尺寸(我希望4个第一个组合框的尺寸最小,让最后一个GroupBox占据剩下的所有空间) 。网格布局不会正确显示我的组框

如下所示,所有内容都显示在同一个“单元格”中。然而,在Blend 4的设计器模式中,看起来好像我的网格显示了5个单元(4个空)...移除行和列声明不会改变一件事情。

Example

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="WpfControlLibrary1.MainControl" 
    x:Name="MultiVol" MinHeight="520.12"> 

    <Grid> 
     <Grid.Background> 
       <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0"> 
        <GradientStop Color="White" Offset="0.966"/> 
        <GradientStop Color="#FFD7D4FF"/> 
       </LinearGradientBrush> 
     </Grid.Background> 

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

     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 

     <GroupBox Margin="8,0" BorderBrush="#FF88B1D8"> 
      <GroupBox.Header> 
       <WrapPanel> 
       <Label Content="General" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" />  
       </WrapPanel> 
      </GroupBox.Header> 

      <UniformGrid Columns="2"> 
       <Label Content="RICs" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Preference" /> 
       <UniformGrid VerticalAlignment="Center" Columns="2" Rows="1"> 
        <RadioButton GroupName="preference" Content="Exotic" IsChecked="False" /> 
        <RadioButton GroupName="preference" Content="Flow" IsChecked="True" /> 
       </UniformGrid> 
       <Label Content="Live updates" /> 
       <CheckBox IsChecked="False" VerticalAlignment="Center"/> 
      </UniformGrid> 
     </GroupBox> 

     <GroupBox Margin="8,0" BorderBrush="#FF88B1D8"> 
      <GroupBox.Header> 
       <WrapPanel> 
       <CheckBox IsChecked="True" VerticalAlignment="Center" /> 
       <Label Content="Volatility" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" /> 
       </WrapPanel> 
      </GroupBox.Header> 
      <UniformGrid Columns="2"> 
       <Label Content="Spots"></Label> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Hist. references" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Tenors" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
      </UniformGrid> 
     </GroupBox> 

     <GroupBox Margin="8,0" BorderBrush="#FF88B1D8"> 
      <GroupBox.Header> 
       <WrapPanel> 
       <CheckBox IsChecked="True" VerticalAlignment="Center" /> 
       <Label Content="Skew" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" /> 
       </WrapPanel> 
      </GroupBox.Header> 
      <UniformGrid Columns="2"> 
       <Label Content="Spot Intervals"></Label> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Hist. references" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Tenors" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Compute 'Power'" /> 
       <CheckBox IsChecked="False" VerticalAlignment="Center"/> 
      </UniformGrid> 
     </GroupBox> 

     <GroupBox Margin="8,0" BorderBrush="#FF88B1D8"> 
      <GroupBox.Header> 
       <WrapPanel> 
       <CheckBox IsChecked="True" VerticalAlignment="Center" /> 
       <Label Content="Term structure" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" /> 
       </WrapPanel> 
      </GroupBox.Header> 
      <UniformGrid Columns="2"> 
       <Label Content="Spots" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
       <Label Content="Tenors" /> 
       <TextBox AcceptsReturn="False" AcceptsTab="True" AllowDrop="True" IsTabStop="True" /> 
      </UniformGrid> 
     </GroupBox> 

     <GroupBox Margin="8,0" BorderBrush="#FF88B1D8"> 
      <GroupBox.Header> 
       <WrapPanel> 
       <Label Content="Live updates" Background="#00000000" Foreground="#FF0033FF" FontWeight="Bold" FontFamily="/WpfControlLibrary1;component/Fonts/#Tahoma" /> 
       </WrapPanel> 
      </GroupBox.Header> 
      <ListView MinHeight="100" Background="{x:Null}"> 
       <ListView.View> 
        <GridView AllowsColumnReorder="False"> 
         <GridViewColumn Header="RIC" /> 
         <GridViewColumn Header="Last tick" /> 
        </GridView> 
       </ListView.View> 
      </ListView> 
     </GroupBox>   
    </Grid> 
</UserControl> 

回答

1

你需要指定GroupBox控制的Grid.Row财产。 Grid不会自动对齐它们。在你的图片中,你的所有控件都在第一个网格行中。将附加属性Grid.Row="0"添加到每个GroupBox以指定该行。请注意,行索引是从零开始的。另外,如果您想要最后一行填满剩余空间,您需要在RowDefinition的其余空间上设置特定高度,或将它们全部设置为自动(使用Height="auto")。使用后者将意味着每行将根据其内容进行调整。将最终的RowDefinition设置为没有明确的高度集将导致其拉伸并填充剩余的可用空间。目前,由于您的RowDefinition都没有设置高度,所以在可用空间内它们的大小将均等。

Here是一个教程,它解释了Grid控件的布局是如何工作的。