BugsFixer
BugsFixer

Reputation: 377

Grid doesn't stretch inside StackPanel

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

Upvotes: 0

Views: 280

Answers (2)

BionicCode
BionicCode

Reputation: 28968

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>

Upvotes: 1

Mohammad Karimi
Mohammad Karimi

Reputation: 457

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.

Upvotes: 0

Related Questions