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 7087399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:40:10+00:00 2026-05-28T07:40:10+00:00

I am trying to create a custom user control in WPF. I want to

  • 0

I am trying to create a custom user control in WPF. I want to be able to set the size manually when I later use the control within another window.

As a short test I have just made a control comprising a canvas within a grid, which totally fills the control. When initialised it draws a rectangle within itself showing its size. I then position this on a window, making it whatever size I want.

However I now have problems, as if I make the height of the rectangle I draw

this.ActualHeight

then when the control initialises this value is still 0, and so I get nothing. If instead I use

this.Height

then I get the height that I made it during design time, and not the size I have subsequently made it within the window.

The height and width seem to be set within the XAML designer, so I don’t know what to do.

Is there an easy way around this?

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-28T07:40:11+00:00Added an answer on May 28, 2026 at 7:40 am

    I think what you’re experiencing is how WPF performs layout and specifically how Canvas does not participate in Layout.

    In your specific case you are setting the width of the Rectangle to Canvas.ActualWidth? Unless the width / height of the canvas have been explictly set then the ActualWidth/Actualheight will be zero, hence you can’t get a point of reference within which to position children of Canvas. What I would do is bind the canvas width and height to its parent container (or set in code) to get the ActualWidth / ActualHeight correctly propagated.

    As a point of interest try this example to understand how the WPF Layout engine works. The following code can force a height on a FrameworkElement to set the Width, Height as normal, then force a layout (measure, arrange pass) on the element in question. This causes the WPF layout engine to measure/arrange the element and propagate the Width, Height to ActualWidth, ActualHeight.

    For example:

    // Set the width, height you want
    element.Width = 123;
    element.Height = 456;
    
    // Force measure/arrange
    element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
    element.Arrange(new Rect(0, 0, element.DesiredWidth, element.DesiredHeight));   
    
    // Subject to sufficient space, the actual width, height will have 
    // the values propagated from width/height after a measure and arrange call
    double width = element.ActualWidth;
    double height = element.ActualHeight;
    

    Also see this related question for clarity. I have used the above method occasionally to measure the text in a textblock so as to position correctly on a canvas.

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

Sidebar

Related Questions

I have a custom WPF user control called a TimeoutPanel that I am trying
I'm trying to create a custom control in WPF to display the game tree
I'm trying to create a custom WPF control and place it inside of a
I am trying to figure out how to use/create a custom control in ASP.NET
I'm trying to create a custom control - a button - which will have
I am trying to create a custom property in a extended control, so that
I'm trying to create a user control that will provide a draggable splitter between
I am trying to host a custom Windows Forms control in WPF. My custom
I'm trying to create a custom search but getting stuck. What I want is
I'm trying to create a legend control that is a databound set of stack

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.