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

时间:2018-07-02 08:25:09

标签: wpf wpf-controls

在下面的标记中,我在Scrollviewer中具有Label。理想情况下,我不想在那儿放DockPanel,而在那儿进行测试(它不能解决问题)。

我希望Scrollviewer填充剩余空间(在列底部放入“发送电子邮件”按钮)。

我的工作似乎无关紧要,我做不到该死的事...

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

1 个答案:

答案 0 :(得分:0)

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

改为使用具有多个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>