0

I have a StackPanel containing a number of UIElements (which have been constructed using a template in from a XAML file) that I wish to display.

To display it in the correctly I need to know its size.

Initially Width/Height/ActualWidth/ActualHeight/DesiredSize are all 0 or NaN. If I call panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)) then its ActualWidth/ActualHeight/DesiredSize are calculated (Width/Height remain as NaN). So far, so good.

However, the ActualWidth / DesiredSize.Width is set to the MinWidth value that was specified in the XAML template, even though, when drawn, the stackpanel is wider than this.

Why does the rendered size not match the ActualWidth/ActualHeight properties?

How can I determine what the rendered size will be so that I can position my panel correctly?

(the panel is for a pop-up tooltip that needs to appear near the mouse pointer, but its position needs adjusting if the tooltip would overflow its allowed area).

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Dave W
  • 1,061
  • 3
  • 16
  • 29
  • When are you reading the value? It HAS to be read after the SizeChanged event got fired, cf the MSDN documentation http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.actualwidth%28VS.95%29.aspx – R4cOOn Feb 17 '11 at 13:45
  • I was reading it after a manual call to UpdateLayout(), which populated ActualHeight/Width & DesiredSzie. However, although they had values they didn't have the final values. LIstening to the SizeChanged event solved that. Thank you. – Dave W Feb 18 '11 at 07:34

1 Answers1

1

You can listen to SizeChanged event of a control to monitor it's size changes. Until a control is rendered, all size properties will be 0/NaN.

decyclone
  • 30,394
  • 6
  • 63
  • 80
  • Awesome, thank you. That did the trick. I'm new to Silverlight and had missed this subtlety while reading the MSDN documentation. – Dave W Feb 18 '11 at 07:32