Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. UserControl does not assume height of parent container

UserControl does not assume height of parent container

Scheduled Pinned Locked Moved WPF
wpfhelpcsharpcssdotnet
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ausadmin
    wrote on last edited by
    #1

    I have been struggling with this issue for some time, and I have now constructed a simple example to demonstate the problem. I am not sure whether this is an issue with WPF or my lack of understanding. As part of our application, I have created a wizard along the lines of Creating an Internationalized Wizard in WPF. This uses the MVVM pattern and the various pages of the wizard are implemented in User Controls. The problem that I have is when the wizard window is resized, the UserControl does not take the height of the window. However it does take the width of the window. The result is that controls such as a list box, which the user may need to have resize with the window, do not resize. I have removed the complexity of our app, and created a very simple example with a window, that just contains a HeaderedContentControl, which contains the model for the UserControl. The window is

    <Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UserControlDemo"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
    <DataTemplate DataType="{x:Type local:SimpleUserControlModel}">
    <local:SimpleUserControl />
    </DataTemplate>
    </Window.Resources>
    <Grid>
    <HeaderedContentControl Name="hcControl" Margin="20" Content="{x:Type local:SimpleUserControl}"
    Height="auto" Width="auto" VerticalContentAlignment="Stretch" />
    </Grid>
    </Window>

    Window code:

    Class Window1
    Private _simpleModel As SimpleUserControlModel

    Public Sub New()
    
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        \_simpleModel = New SimpleUserControlModel
        hcControl.Content = \_simpleModel
    End Sub
    

    End Class

    User control:

    <UserControl x:Class="SimpleUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid>
    <Border Margin="5" BorderThickness="5" BorderBrush="Red" />
    </Grid>
    </UserControl>

    User control model:

    Public Class SimpleUserControlMod

    M 1 Reply Last reply
    0
    • A ausadmin

      I have been struggling with this issue for some time, and I have now constructed a simple example to demonstate the problem. I am not sure whether this is an issue with WPF or my lack of understanding. As part of our application, I have created a wizard along the lines of Creating an Internationalized Wizard in WPF. This uses the MVVM pattern and the various pages of the wizard are implemented in User Controls. The problem that I have is when the wizard window is resized, the UserControl does not take the height of the window. However it does take the width of the window. The result is that controls such as a list box, which the user may need to have resize with the window, do not resize. I have removed the complexity of our app, and created a very simple example with a window, that just contains a HeaderedContentControl, which contains the model for the UserControl. The window is

      <Window x:Class="Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:UserControlDemo"
      Title="Window1" Height="300" Width="300">
      <Window.Resources>
      <DataTemplate DataType="{x:Type local:SimpleUserControlModel}">
      <local:SimpleUserControl />
      </DataTemplate>
      </Window.Resources>
      <Grid>
      <HeaderedContentControl Name="hcControl" Margin="20" Content="{x:Type local:SimpleUserControl}"
      Height="auto" Width="auto" VerticalContentAlignment="Stretch" />
      </Grid>
      </Window>

      Window code:

      Class Window1
      Private _simpleModel As SimpleUserControlModel

      Public Sub New()
      
          ' This call is required by the Windows Form Designer.
          InitializeComponent()
      
          ' Add any initialization after the InitializeComponent() call.
          \_simpleModel = New SimpleUserControlModel
          hcControl.Content = \_simpleModel
      End Sub
      

      End Class

      User control:

      <UserControl x:Class="SimpleUserControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
      <Grid>
      <Border Margin="5" BorderThickness="5" BorderBrush="Red" />
      </Grid>
      </UserControl>

      User control model:

      Public Class SimpleUserControlMod

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The default template for a HeaderedContentControl is

      <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
          <StackPanel>
              <ContentPresenter ContentSource="Header"/>
              <ContentPresenter />
          </StackPanel>
      </ControlTemplate>
      

      which gives you the behavior you're seeing, because they used a StackPanel. I would use a grid in the HeaderedContentControl template instead. Here's an example (simplifying your example even more):

      <Window.Resources>
          <Style x:Key="HeaderedContentControlStyle1" TargetType="{x:Type HeaderedContentControl}">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                          <Grid>
                              <Grid.RowDefinitions>
                                  <RowDefinition Height="Auto" />
                                  <RowDefinition Height="\*" />
                              </Grid.RowDefinitions>
                              <ContentPresenter Grid.Row="0" ContentSource="Header"/>
                              <ContentPresenter Grid.Row="1" />
                          </Grid>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </Window.Resources>
      
      <Grid>
          <HeaderedContentControl Name="hcControl" Margin="20" Style="{DynamicResource HeaderedContentControlStyle1}" >
              <Grid>
                  <Border Margin="5" BorderThickness="5" BorderBrush="Red" />
              </Grid>
          </HeaderedContentControl>
      </Grid>
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      modified on Tuesday, May 19, 2009 12:48 PM

      A M 2 Replies Last reply
      0
      • M Mark Salsbery

        The default template for a HeaderedContentControl is

        <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
            <StackPanel>
                <ContentPresenter ContentSource="Header"/>
                <ContentPresenter />
            </StackPanel>
        </ControlTemplate>
        

        which gives you the behavior you're seeing, because they used a StackPanel. I would use a grid in the HeaderedContentControl template instead. Here's an example (simplifying your example even more):

        <Window.Resources>
            <Style x:Key="HeaderedContentControlStyle1" TargetType="{x:Type HeaderedContentControl}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="\*" />
                                </Grid.RowDefinitions>
                                <ContentPresenter Grid.Row="0" ContentSource="Header"/>
                                <ContentPresenter Grid.Row="1" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        
        <Grid>
            <HeaderedContentControl Name="hcControl" Margin="20" Style="{DynamicResource HeaderedContentControlStyle1}" >
                <Grid>
                    <Border Margin="5" BorderThickness="5" BorderBrush="Red" />
                </Grid>
            </HeaderedContentControl>
        </Grid>
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        modified on Tuesday, May 19, 2009 12:48 PM

        A Offline
        A Offline
        ausadmin
        wrote on last edited by
        #3

        Mark, Thanks for the simple solution to this problem! I can see now that I needed to have a style with a control template to define the behaviour of the HeaderedContentControl. Reading the help page again, I can see I missed "A HeaderedContentControl has a limited default style. An application developer can create a HeaderedContentControl, but its appearance will be very simple." Note that we need to use a UserControl, to represent a number of more complex "pages", which are swapped in / out in code as the user steps through the wizard. That is why we need the user control rather than the simpler approach you presented. I will implement this back in our application and hopefully it will solve the original problem. Regards, Tim

        M 1 Reply Last reply
        0
        • A ausadmin

          Mark, Thanks for the simple solution to this problem! I can see now that I needed to have a style with a control template to define the behaviour of the HeaderedContentControl. Reading the help page again, I can see I missed "A HeaderedContentControl has a limited default style. An application developer can create a HeaderedContentControl, but its appearance will be very simple." Note that we need to use a UserControl, to represent a number of more complex "pages", which are swapped in / out in code as the user steps through the wizard. That is why we need the user control rather than the simpler approach you presented. I will implement this back in our application and hopefully it will solve the original problem. Regards, Tim

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          ausadmin wrote:

          That is why we need the user control rather than the simpler approach you presented.

          Right, I figured that. I just wrapped it all into simple XAML so you could copy/paste it and try it out. The key point was the custom template :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • M Mark Salsbery

            The default template for a HeaderedContentControl is

            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <StackPanel>
                    <ContentPresenter ContentSource="Header"/>
                    <ContentPresenter />
                </StackPanel>
            </ControlTemplate>
            

            which gives you the behavior you're seeing, because they used a StackPanel. I would use a grid in the HeaderedContentControl template instead. Here's an example (simplifying your example even more):

            <Window.Resources>
                <Style x:Key="HeaderedContentControlStyle1" TargetType="{x:Type HeaderedContentControl}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="\*" />
                                    </Grid.RowDefinitions>
                                    <ContentPresenter Grid.Row="0" ContentSource="Header"/>
                                    <ContentPresenter Grid.Row="1" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Window.Resources>
            
            <Grid>
                <HeaderedContentControl Name="hcControl" Margin="20" Style="{DynamicResource HeaderedContentControlStyle1}" >
                    <Grid>
                        <Border Margin="5" BorderThickness="5" BorderBrush="Red" />
                    </Grid>
                </HeaderedContentControl>
            </Grid>
            

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            modified on Tuesday, May 19, 2009 12:48 PM

            M Offline
            M Offline
            mick codeproject
            wrote on last edited by
            #5

            Thank you for saving my day!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups