Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How to get Height and Width of a control Created at runtime.
8 replies. Latest Post by VladF on May 30, 2008.
(0)
rajivkt
Member
14 points
14 Posts
05-29-2008 6:39 AM |
I am creating a grid Control at runtime and using the popup class to show that grid on runtime as popup. But i am not able to get the Height and Width of the Grid. I want it becuse depending on the mouse pointer on the screen i want to show it just beneath the mouse if there is space otherwise i want to show it on top. just like Tool Tip. But i am not able to get the width and height of the Grid Control. it is always returning 0
thierry....
Contributor
2054 points
280 Posts
05-29-2008 6:59 AM |
You should be able to retreive the Width and Height of your grid by using the ActualHeight and ActualWidth properties.
05-29-2008 7:24 AM |
I tried that also but still not able to get it. If try Grid.Width it is giving NAN and if i use Grid.ActualWidth then it is giving 0.0. Actually i am creating the Grid at run time and adding the row in runtime.
sladapter
All-Star
17441 points
3,172 Posts
05-29-2008 9:42 AM |
That's depending on what container you put the control in. I found if I put Grid or other Control in Canvas, I need to specify Width/Height. Otherwise it will have size 0 unless it has content in it has size (then it will be the size of the content). I think this is because Canvas is mainly for layout controls by using absolute position. So I need to specify where I want put them and what size they should have. If I put Grid in a Grid or other Panel controls and not specify Width/Height, the Grid will have the size of it's container.
Is your Grid in a Canvas?
05-29-2008 9:58 AM |
If your grid is displayed on screen, it has an ActualWidth / ActualHeight. If you get 0 from those properties it should be due to timing problem. If you retreive the value before the layout is processed, your grid will have a size of 0.
Dave Relyea
Participant
1084 points
249 Posts
05-29-2008 3:42 PM |
If you don't set the height and/or width at runtime, and are relying on layout to do it for you, you have to wait until layout runs, or you may be able to call Measure on the control yourself. Then the ActualWidth/ActualHeight properties will be set.
05-30-2008 6:22 AM |
Which event is fired after the layout is run. how to trap that event?
05-30-2008 9:16 AM |
You probably want the SizeChanged event, and not the LayoutUpdated event. This post explains the difference and how to use them:
http://blogs.msdn.com/devdave/archive/2008/05/27/layout-events-sizechanged-and-layoutupdated.aspx
VladF
216 points
87 Posts
05-30-2008 9:34 PM |
I think you can get the size of Popup's child after you assign true to IsOpen. Something like this should work:
var popup = new Popup();popup.Child = separator;popup.IsOpen = true;separator.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));var size = separator.DesiredSize;