Почему мой контейнер не занимает все доступное пространство в WPF? - PullRequest
0 голосов
/ 02 июля 2018

В разметке ниже у меня есть ярлык в Scrollviewer. В идеале я не хочу, чтобы там была 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 голосов
/ 02 июля 2018

не используйте StackPanel, если вы хотите, чтобы какой-то элемент растягивался по вертикали. StackPanel может предложить бесконечное вертикальное пространство для аранжировки, но не имеет смысла растягивать до бесконечности, поэтому используется минимально возможный размер.

вместо этого используйте Grid с несколькими RowDefinitions и поместите растягивающий элемент в ряд с 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>
...