Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:50:35+00:00 2026-05-10T14:50:35+00:00

When creating a UserControl in WPF, I find it convenient to give it some

  • 0

When creating a UserControl in WPF, I find it convenient to give it some arbitrary Height and Width values so that I can view my changes in the Visual Studio designer. When I run the control, however, I want the Height and Width to be undefined, so that the control will expand to fill whatever container I place it in. How can I acheive this same functionality without having to remove the Height and Width values before building my control? (Or without using DockPanel in the parent container.)

The following code demonstrates the problem:

<Window x:Class='ExampleApplication3.Window1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:loc='clr-namespace:ExampleApplication3'     Title='Example' Height='600' Width='600'>     <Grid Background='LightGray'>         <loc:UserControl1 />     </Grid> </Window> 

The following definition of UserControl1 displays reasonably at design time but displays as a fixed size at run time:

<UserControl x:Class='ExampleApplication3.UserControl1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     Height='300' Width='300'>     <Grid Background='LightCyan' /> </UserControl> 

The following definition of UserControl1 displays as a dot at design time but expands to fill the parent Window1 at run time:

<UserControl x:Class='ExampleApplication3.UserControl1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>     <Grid Background='LightCyan' /> </UserControl> 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:50:35+00:00Added an answer on May 10, 2026 at 2:50 pm

    In Visual Studio add the Width and Height attribute to your UserControl XAML, but in the code-behind insert this

    public UserControl1() {     InitializeComponent();     if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)     {         this.Width = double.NaN; ;         this.Height = double.NaN; ;     } } 

    This checks to see if the control is running in Design-mode. If not (i.e. runtime) it will set the Width and Height to NaN (Not a number) which is the value you set it to if you remove the Width and Height attributes in XAML.

    So at design-time you will have the preset width and height (including if you put the user control in a form) and at runtime it will dock depending on its parent container.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in WPF and I am creating some userControl which some of
I'm creating a UserControl that's a specialized ListBox/View (type not relevant). Now I'm faced
I'm looking for some guidance on creating a WPF UserControl. My objective is to
I am creating a WPF-usercontrol that needs to check that new UIElements does not
Given that you're creating a User Control in WPF that will be displayed on
I'm looking for a tutorial that explains creating custom usercontrols in WPF. I want
I am creating a series of UserControls that all have some similar business logic.
I have a WPF UserControl that I'd like to inject dependencies into. What's the
New to WPF. I am creating UserControls that need read access to the ViewModel
I am creating a custom WPF control that uses an image inside of it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.