Get Width/Height of UserControl or Canvas? RRS feed

  • Question

  • I have a UserControl that is drawing to an embedded canvas object.

    How do I get the width/height of the canvas object or the UserControl? I have tried using this.Width (where this is the UserControl) and myCanvasObject.Width and both values are NaN. I also tried ActualWidth and that is always 0.0.

    The UserControl can be resized by the owning window and I'd like to be able to get the actual size.
    Saturday, August 2, 2008 5:29 PM


All replies

  • The Width and Height properties are used by the rendering engine to determine how wide/tall the control wants to be.  If you want to know how tall it actually is, you can use the ActualWidth and ActualHeight properties.

    Hope this helps,

    Brian Schwalm • www.anythinksolutions.com
    • Proposed as answer by Brian Schwalm Sunday, August 3, 2008 8:13 PM
    Saturday, August 2, 2008 8:24 PM
  • He said he did try with ActualWidth and ActualHeight properties, so it probably has to be because he is asking for their values too soon in the lifecycle of the control. See more in this thread: http://forums.msdn.microsoft.com/en-US/wpf/thread/ca22da25-7aad-4041-8088-181c32b37909

    • Marked as answer by Marco Zhou Friday, August 8, 2008 10:05 AM
    Monday, August 4, 2008 9:18 AM