为什么我的容器没有占用 WPF 中所有可用的 space?

Why isn't my container occupying all available space in WPF?

在下面的标记中,我在滚动查看器中有标签。理想情况下,我不想让 DockPanel 在那里,但它在那里用于测试(它没有解决问题)。

我希望滚动查看器填充剩余的 space(将 "Send Email" 按钮放在列的底部)。

我做什么似乎无关紧要,我做不到这该死的事情...

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0">
        <ComboBox ItemsSource="{Binding Campaigns}"
                  DisplayMemberPath="Name"
                  HorizontalAlignment="Stretch" VerticalAlignment="Top"
                  IsEditable="True" IsReadOnly="True"
                  SelectedItem="{Binding SelectedCampaign}"
                  Text="Select a Campaign"/>
        <ComboBox ItemsSource="{Binding Emails}"
                  DisplayMemberPath="Subject"
                  HorizontalAlignment="Stretch" VerticalAlignment="Top"
                  IsEditable="True" IsReadOnly="True"
                  SelectedItem="{Binding SelectedEmail}"
                  Text="Select an Email"/>
        <ComboBox ItemsSource="{Binding Groups}"
                  DisplayMemberPath="Name"
                  HorizontalAlignment="Stretch" VerticalAlignment="Top"
                  IsEditable="True" IsReadOnly="True"
                  SelectedItem="{Binding SelectedGroup}"
                  Text="Select a Contact Group"/>
        <Button Command="{Binding LoadContactsCommand}"
                CommandParameter="{Binding SelectedGroup}"
                HorizontalAlignment="Stretch" VerticalAlignment="Top"
                ToolTip="Refresh the current list of contacts." Content="Load Contacts"/>
        <DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <ScrollViewer DockPanel.Dock="Bottom" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <Label Content="{Binding SendStatus, UpdateSourceTrigger=PropertyChanged}"
                       Height="auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
            </ScrollViewer>
        </DockPanel>
        <Button Command="{Binding SendEmailsCommand}" Content="Send Emails"
                VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
    </StackPanel>
</Grid>

如果您希望某些元素垂直拉伸,请不要使用 StackPanel。 StackPanel 可以为排列提供无限垂直 space,但拉伸到无限没有意义,因此使用了尽可能小的尺寸。

改为使用具有多个 RowDefinition 的 Grid,并使用 Height="*":

将拉伸元素放在一行中
<Grid Grid.Column="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <ComboBox ItemsSource="{Binding Campaigns}" Grid.Row="0"
              DisplayMemberPath="Name"
              HorizontalAlignment="Stretch" VerticalAlignment="Top"
              IsEditable="True" IsReadOnly="True"
              SelectedItem="{Binding SelectedCampaign}"
              Text="Select a Campaign"/>
    <ComboBox ItemsSource="{Binding Emails}"  Grid.Row="1"
              DisplayMemberPath="Subject"
              HorizontalAlignment="Stretch" VerticalAlignment="Top"
              IsEditable="True" IsReadOnly="True"
              SelectedItem="{Binding SelectedEmail}"
              Text="Select an Email"/>
    <ComboBox ItemsSource="{Binding Groups}"  Grid.Row="2"
              DisplayMemberPath="Name"
              HorizontalAlignment="Stretch" VerticalAlignment="Top"
              IsEditable="True" IsReadOnly="True"
              SelectedItem="{Binding SelectedGroup}"
              Text="Select a Contact Group"/>
    <Button Command="{Binding LoadContactsCommand}" Grid.Row="3"
            CommandParameter="{Binding SelectedGroup}"
            HorizontalAlignment="Stretch" VerticalAlignment="Top"
            ToolTip="Refresh the current list of contacts." Content="Load Contacts"/>

    <ScrollViewer Grid.Row="4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Label Content="{Binding SendStatus, UpdateSourceTrigger=PropertyChanged}"
               Height="auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
    </ScrollViewer>

    <Button Grid.Row="5" Command="{Binding SendEmailsCommand}" Content="Send Emails"
            VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
</Grid>