8

我有一个带有一些垂直对齐控件的 StackPanel(此图片右侧的堆栈面板: 在此处输入图像描述

最后一个控件应始终放置在边框控件内的窗口底部(图片上的确定按钮)。在 QT 中,我会插入一个间隔控件来按下按钮。我如何在 WPF 中做到这一点?谢谢你。这是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>
4

1 回答 1

2

在 WPF 中有很多方法可以实现这一点。如下图之一:

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

上面的 XAML,用于替换 XAML 中的右手Border将产生以下结果,并且在调整窗口大小时将按钮保持在底部。

在此处输入图像描述

于 2013-09-09T15:24:11.807 回答