WPF Controls do not resize inside container

When you try to put a WPF User Control or a Page inside of a container you will notice the control is not resizing to the parent it is hosted in. Example, a page that is hosted in a frame.

This is due to the size you have set to the control or page.

 <Page x:Class=”LAD_Project_Planning_WPF.pProjectCard” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221; xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;  Title=”pProjectCard” Loaded=”Page_Loaded” Height=”382″ Width=”789″>


As you can see the page’s height and width are set to 382 and 789 pixels. This will cause the control to always have these dimensions. It doesn’t matter if it is set to strech inside of a container or not.

Removing the dimensions from the control will solve this issue at runtime, but you won’t be able to see the control in design time.

The only solution I found until now was to change the dimensions in the OnLoad event.

if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
    this.Width = double.NaN; 
    this.Height = double.NaN;

Tagged with: ,
Posted in .NET
One comment on “WPF Controls do not resize inside container
  1. D says:

    use d:DesignHeight and Width

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: