Determining the size of a UIElement in Silverlight

1.1k views Asked by At

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).

1

There are 1 answers

1
decyclone On BEST ANSWER

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.