2

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Greg
  • 1,673
  • 4
  • 20
  • 27

1 Answers1

1

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.

Community
  • 1
  • 1
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • 1
    Thanks again. I also found another way by accident, which was by initiating a redraw after a SizeChanged() event on my control, which I need to happen anyway for screen resizing. – Greg Jan 23 '12 at 10:19