Question

I have a StackPanel with some vertically alligned controls (stackpanel on the right in this picture: enter image description here

The last control should always be placed at the bottom of the window inside of the border control (The OK button on the picture). In QT I'd insert a spacer control to push the button down. How do I do that in WPF? Thank you. Here is the xaml:

<Window x:Class="Test.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow" Height="350" Width="525">
    <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="354*" />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Border Margin="5" Background="Gray" CornerRadius="6" BorderBrush="Gray">
      <Viewport3D Grid.Column="0" Name="viewport" ClipToBounds="True">
      </Viewport3D>
    </Border>

    <Border Grid.Column="1" CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="1" Margin="5" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Stretch" >
      <StackPanel>
        <StackPanel Orientation="Horizontal">
          <Label Content="Dimension" Name="label1" VerticalAlignment="Center" />
          <ComboBox Text="3D" HorizontalAlignment="Right" Name="dimensioncb" VerticalAlignment="Center" IsReadOnly="True" IsEditable="True">
            <ComboBoxItem>2D</ComboBoxItem>
            <ComboBoxItem>3D</ComboBoxItem>
          </ComboBox>
        </StackPanel>
        <Separator/>
        <DockPanel>
          <Label Content="Iteration" Name="label2" VerticalAlignment="Center" />
          <ComboBox Text="3" HorizontalAlignment="Right" Name="iterationcb" VerticalAlignment="Center" IsReadOnly="True" IsEditable="True">
            <ComboBoxItem>1</ComboBoxItem>
            <ComboBoxItem>2</ComboBoxItem>
            <ComboBoxItem>3</ComboBoxItem>
            <ComboBoxItem>4</ComboBoxItem>
            <ComboBoxItem>5</ComboBoxItem>
            <ComboBoxItem>6</ComboBoxItem>
            <ComboBoxItem>7</ComboBoxItem>
            <ComboBoxItem>8</ComboBoxItem>
          </ComboBox>
        </DockPanel>
        <Button Content="OK" VerticalAlignment="Bottom"/>
      </StackPanel>
    </Border>

  </Grid>
</Window>
Was it helpful?

Solution

There are a bunch of ways that you can accomplish this in WPF. Shown below is one:

<Border Grid.Column="1" CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="1" Margin="5" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Stretch" >
  <!--Add a grid control to separate your stackpanel and button-->
  <Grid>
    <Grid.RowDefinitions>
       <RowDefinition Height="*"/>
       <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
  <StackPanel Grid.Row="0">
    <StackPanel Orientation="Horizontal">
      <Label Content="Dimension" Name="label1" VerticalAlignment="Center" />
      <ComboBox Text="3D" HorizontalAlignment="Right" Name="dimensioncb" VerticalAlignment="Center" IsReadOnly="True" IsEditable="True">
        <ComboBoxItem>2D</ComboBoxItem>
        <ComboBoxItem>3D</ComboBoxItem>
      </ComboBox>
    </StackPanel>
    <Separator/>
    <DockPanel>
      <Label Content="Iteration" Name="label2" VerticalAlignment="Center" />
      <ComboBox Text="3" HorizontalAlignment="Right" Name="iterationcb" VerticalAlignment="Center" IsReadOnly="True" IsEditable="True">
        <ComboBoxItem>1</ComboBoxItem>
        <ComboBoxItem>2</ComboBoxItem>
        <ComboBoxItem>3</ComboBoxItem>
        <ComboBoxItem>4</ComboBoxItem>
        <ComboBoxItem>5</ComboBoxItem>
        <ComboBoxItem>6</ComboBoxItem>
        <ComboBoxItem>7</ComboBoxItem>
        <ComboBoxItem>8</ComboBoxItem>
      </ComboBox>
    </DockPanel>
  </StackPanel>
    <Button Grid.Row="1" Content="OK" VerticalAlignment="Bottom"/>
  </Grid>
</Border>

The above XAML, used to replace the right-hand Border in your XAML will produce the following result, and will keep the button on the bottom when re-sizing the window.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top