wpf 如何让 UserControl 伸展以填充分配的空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1031013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:36:36  来源:igfitidea点击:

How to get a UserControl to stretch to fill the alloted space?

wpfxamlautosize

提问by Edward Tanguay

In a 630 x 400 Window, I'm loading there XAML elements:

在 630 x 400 窗口中,我正在加载 XAML 元素:

  • menu at top
  • dynamic user control
  • footer at the bottom
  • 顶部菜单
  • 动态用户控制
  • 底部的页脚

The problem is that when I set the background of the UserControl, the color only goes down as far as the content. I want the background of the UserControl to cover the whole UserControl of course. I've tried:

问题是,当我设置 UserControl 的背景时,颜色只会下降到 content。我当然希望 UserControl 的背景覆盖整个 UserControl。我试过了:

  • VerticalContentAlignment="Stretch" in the UserControl
  • VerticalAlignment="Stretch" in the UserControl
  • VerticalContentAlignment="Stretch" in the MainView
  • VerticalAlignment="Stretch" in the MainView
  • 用户控件中的VerticalContentAlignment="Stretch"
  • 用户控件中的VerticalAlignment="Stretch"
  • 视图中的VerticalContentAlignment="Stretch"
  • 视图中的VerticalAlignment="Stretch"

But the color still refuses to go down. I don't want to set a fixed width since the user is able to increase the size of the application.

但颜色仍然拒绝下降。我不想设置固定宽度,因为用户可以增加应用程序的大小。

How can I get the background color of my UserControl to fill the full area of the UserControlinstead of only the area of its content?

如何让我的 UserControl 的背景颜色填充 UserControl 的整个区域,而不仅仅是其内容区域?

PageItemOptionsView.xaml:

PageItemOptionsView.xaml:

<UserControl x:Class="TestMenu234.Views.PageItemOptionsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             VerticalContentAlignment="Stretch"
             VerticalAlignment="Stretch"
             Background="#ddd">
    <StackPanel Margin="10">
        <TextBlock Text="This is the options area."/>
        <Button Content="Click to go to the Manage Customers page."
                    Width="200"/>
    </StackPanel>
</UserControl>

MainView.xaml:

主视图.xaml:

<Window x:Class="TestMenu234.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestMenu234.Commands"
    xmlns:vm="clr-namespace:TestMenu234.ViewModels"
    xmlns:v="clr-namespace:TestMenu234.Views"
    Title="Main Window" Height="400" Width="630" MinWidth="630">

...

...

    <DockPanel LastChildFill="False">

        <Menu DockPanel.Dock="Top">
            <MenuItem 
                Header="Pages" ItemsSource="{Binding AllPageItemViewModels}"
                      ItemTemplate="{StaticResource CodeGenerationMenuTemplate}"/>
        </Menu>

        <ContentControl
            DockPanel.Dock="Top"
            VerticalAlignment="Stretch"
            VerticalContentAlignment="Stretch"
            Content="{Binding CurrentPageItemViewModel}"/>

        <Border DockPanel.Dock="Bottom" Padding="5 5 5 0" Background="#eee">
            <Grid Background="#eee">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" MinWidth="300"/>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>
                <Slider 
                Grid.Column="0"
                HorizontalAlignment="Left"
                Value="{Binding CurrentPageItemViewModelIndex}"
                Width="300"
                Minimum="0"
                Maximum="{Binding HighestPageItemIndex}"/>

                <TextBlock Grid.Column="1" 
                           HorizontalAlignment="Center" FontWeight="Bold" 
                           Text="{Binding CurrentPageItemViewModelTitle}"/>

                <DockPanel Grid.Column="2" Margin="0 0 0 5" LastChildFill="False">
                    <Button
                    Margin="3 0 0 0"
                    DockPanel.Dock="Right"
                HorizontalAlignment="Right"
                Content="Next" Command="{Binding NextPageCommand}"/>
                    <Button
                    DockPanel.Dock="Right"
                Content="Prev" Command="{Binding PreviousPageCommand}"/>
                </DockPanel>
            </Grid>
        </Border>

    </DockPanel>
</Window>

采纳答案by Fenton

Have you tried...

你有没有尝试过...

  1. Setting Margin="0"
  2. Making your control the last child of DockPanel with LastChildFill="True"
  1. 设置边距="0"
  2. 使用 LastChildFill="True" 使您的控件成为 DockPanel 的最后一个子项