Grid doesn't stretch inside StackPanel

烈酒焚心 提交于 2020-03-05 04:00:48

问题


I have two textboxes with constant height, I want to vertically stack the textboxes and a grid. I tried with stackpanel but then the grid doesn't stretch and it stays with same size all the time (smallest as possible).

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

I also tried to use Grid instead of stackpanel but then when I make the app with full screen there is a margin between the textboxes and the internal grid


回答1:


StackPanel doesn't behave like this. It occupies minimal required space. Use a Grid instead and define RowDefinitions. By default the space inside a Grid is equally distributed among the rows (Height is set to "*"), so you have to set the Height to "Auto" where you want the rows to occupy minimum space:

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

Alternatively try a 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>



回答2:


You can achieve this by using an 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();
    }
}

You can introduce your converter in XAML in the resources section:

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

And your XAML will be changed to:

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

Definitely, the backgrounds colors are just for more clarification and not necessary.



来源:https://stackoverflow.com/questions/53977945/grid-doesnt-stretch-inside-stackpanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!