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

22,953

Have you tried...

  1. Setting Margin="0"
  2. Making your control the last child of DockPanel with LastChildFill="True"
Share:
22,953

Related videos on Youtube

Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on August 03, 2020

Comments

  • Angry Dan
    Angry Dan almost 4 years

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

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

    • VerticalContentAlignment="Stretch" in the UserControl
    • VerticalAlignment="Stretch" in the UserControl
    • VerticalContentAlignment="Stretch" in the MainView
    • VerticalAlignment="Stretch" in the MainView

    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 UserControl instead of only the area of its content?

    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>