Skip to main content

Microsoft Silverlight

Answered Question How to get Height and Width of a control Created at runtime.RSS Feed

(0)

rajivkt
rajivkt

Member

Member

14 points

14 Posts

How to get Height and Width of a control Created at runtime.

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.bouquain
thierry....

Contributor

Contributor

2054 points

280 Posts

Silverlight MVP

Re: How to get Height and Width of a control Created at runtime.

You should be able to retreive the Width and Height of your grid by using the ActualHeight and ActualWidth properties.

Thierry Bouquain
Ucaya
http://www.ucaya.com

rajivkt
rajivkt

Member

Member

14 points

14 Posts

Re: How to get Height and Width of a control Created at runtime.

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
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: How to get Height and Width of a control Created at runtime.

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? 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

thierry.bouquain
thierry....

Contributor

Contributor

2054 points

280 Posts

Silverlight MVP

Re: How to get Height and Width of a control Created at runtime.

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.

Thierry Bouquain
Ucaya
http://www.ucaya.com

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

Re: How to get Height and Width of a control Created at runtime.

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.

rajivkt
rajivkt

Member

Member

14 points

14 Posts

Re: How to get Height and Width of a control Created at runtime.

Which event is fired after the layout is run. how to trap that event?

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft
Answered Question

Re: How to get Height and Width of a control Created at runtime.

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
VladF

Member

Member

216 points

87 Posts

Answered Question

Re: How to get Height and Width of a control Created at runtime.

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;

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities