网格不在StackPanel内部伸展

时间:2018-12-30 13:20:52

标签: c# wpf xaml

我有两个高度恒定的文本框,我想垂直堆叠这些文本框和一个网格。我尝试过使用stackpanel,但是网格没有拉伸,并且始终保持相同大小(尽可能最小)。

<StackPanel Orientation="Vertical" MaxWidth="110">
        <TextBox  Background="White" Height="40" Text="some text1"/>
        <TextBox  Background="White" Height="40" Text="some text2"/>
        <Grid x:Name="internalGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" MaxHeight="300"/>
                <RowDefinition Height="1*" MaxHeight="300"/>
            </Grid.RowDefinitions>
            <Button Grid.Column="0" Grid.Row="0"/>
            <Button Grid.Column="0" Grid.Row="1"/>
            <Button Grid.Column="1" Grid.Row="0"/>
            <Button Grid.Column="1" Grid.Row="1"/>
        </Grid>
    </StackPanel>

我也尝试使用Grid而不是stackpanel,但是当我使应用全屏显示时,文本框和内部网格之间会有一个空白

2 个答案:

答案 0 :(得分:1)

StackPanel并非如此。它占用的空间最小。请改用网格并定义RowDefinition。默认情况下,网格内的空间在行之间平均分配(高度设置为“ *”),因此您必须将高度设置为“自动”,以使行占据最小空间:

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

    <TextBox Grid.Row="0" Background="White"
              Height="40"
              Text="some text1" />
    <TextBox Grid.Row="1" Background="White"
              Height="40"
              Text="some text2" />

    <Grid Grid.Row="2" x:Name="internalGrid">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="1*"
                       MaxHeight="300" />
        <RowDefinition Height="1*"
                       MaxHeight="300" />
      </Grid.RowDefinitions>

      <Button Grid.Column="0"
              Grid.Row="0" />
      <Button Grid.Column="0"
              Grid.Row="1" />
      <Button Grid.Column="1"
              Grid.Row="0" />
      <Button Grid.Column="1"
              Grid.Row="1" />
    </Grid>
  </Grid>

或者尝试使用DockPanel

<DockPanel MaxWidth="110" LastChildFill="True" >
    <TextBox DockPanel.Dock="Top" Background="White"
              Height="40"
              Text="some text1" />
    <TextBox DockPanel.Dock="Top" Background="White"
              Height="40"
              Text="some text2" />
    <Grid DockPanel.Dock="Top" x:Name="internalGrid">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
        <ColumnDefinition Width="1*"
                          MaxWidth="110" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="1*"
                       MaxHeight="300" />
        <RowDefinition Height="1*"
                       MaxHeight="300" />
      </Grid.RowDefinitions>
      <Button Grid.Column="0"
              Grid.Row="0" />
      <Button Grid.Column="0"
              Grid.Row="1" />
      <Button Grid.Column="1"
              Grid.Row="0" />
      <Button Grid.Column="1"
              Grid.Row="1" />
    </Grid>
  </DockPanel>

答案 1 :(得分:0)

您可以使用IMultiValueConverter来实现:

[ValueConversion(typeof(double), typeof(double))]
public class MyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var total = (double)values.FirstOrDefault();
        var subtract = values.Cast<double>().Sum();
        return total + total - subtract;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您可以在参考资料部分中的XAML中介绍您的转换器:

<Window.Resources>
    <local:MyConverter x:Key="Converter"></local:MyConverter>
</Window.Resources>

您的XAML将会更改为:

<StackPanel Orientation="Vertical" MaxWidth="110" Background="Red">
        <TextBox Name="Label1" Background="White" Height="40" Text="some text1"/>
        <TextBox Name="Label2" Background="White" Height="40" Text="some text2"/>
        <Grid x:Name="internalGrid" Background="Yellow" >
            <Grid.Height>
                <MultiBinding Converter="{StaticResource Converter}">
                    <Binding RelativeSource="{RelativeSource AncestorType=StackPanel}" Path="ActualHeight"/>
                    <Binding ElementName="Label1" Path="ActualHeight"/>
                    <Binding ElementName="Label2" Path="ActualHeight"/>
                </MultiBinding>
            </Grid.Height>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
                <ColumnDefinition Width="1*" MaxWidth="110"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" MaxHeight="300"/>
                <RowDefinition Height="1*" MaxHeight="300"/>
            </Grid.RowDefinitions>
        </Grid>
    </StackPanel>

可以肯定的是,背景颜色只是为了澄清,而不是必需的。