wpf - 如何让 UserControl 拉伸(stretch)以填充分配的空间?

标签 wpf xaml autosize

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

  • 顶部菜单
  • 动态用户控制
  • 底部页脚

  • 问题是当我设置 UserControl 的背景时,颜色只下降到内容 .我当然希望 UserControl 的背景覆盖整个 UserControl。我试过了:
  • 垂直内容对齐 中的="拉伸(stretch)"用户控制
  • 垂直对齐 中的="拉伸(stretch)"用户控制
  • 垂直内容对齐 中的="拉伸(stretch)"主视图
  • 垂直对齐 中的="拉伸(stretch)"主视图

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

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

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

    最佳答案

    你有没有尝试过...

  • 设置边距="0"
  • 使用 LastChildFill="True"
  • 使您的控件成为 DockPanel 的最后一个子项

    关于wpf - 如何让 UserControl 拉伸(stretch)以填充分配的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1031013/

    相关文章:

    wpf - x :Key is ignored inside MergedDictionaries

    c# - WPF 数据绑定(bind)架构问题

    wpf - 通过代码设置TextBox.Width ="*"

    ios - 使用 Xamarin Studio 在 iOS 中实现 'About' View

    c# - WPF图像源来自常量

    wpf - 全职WPF开发

    c# - 使用 WPF 将 Enter 键发送到外部应用程序时出现问题

    c# - WPF XAML 字符串格式日期时间 : Output in wrong culture?

    css - Kendo UI 网格列自动调整大小

    wpf - 如何确定 MediaElement 是否正在播放?