【问题标题】:Why isn't my container occupying all available space in WPF?为什么我的容器没有占用 WPF 中的所有可用空间?
【发布时间】:2018-07-02 08:25:09
【问题描述】:

在下面的标记中,我在 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>

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

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

    使用具有多个 RowDefinitions 的 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>
    

    【讨论】:

    • 完美!谢谢 :) 不知道为什么我没有想到它,但这实际上甚至比 StackPanel 看起来更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2020-01-15
    • 2021-08-24
    相关资源
    最近更新 更多