Quick Silverlight tip: Why my ActualWidth and ActualHeight equal 0?

If you’ll try to use ActualWidth and ActualHight of controls with explicitly set Width and Height, you’ll not see any problem, however, if your control sits inside other control, ActualWidth and ActualHeight properties will be equal to 0. Why this and how to fix it?

Actually, measurement and layout pass in Silverlight run asynchronously, thus it executed with or, even after, your code. So in order to fix it, you should measure ActualWidth and Actual Height asynchronously. So, instead of:

//get and use ActualWidth/ActualHeight

Use:

Dispatcher.BeginInvoke(delegate
{
  //get and use ActualWidth/ActualHeight
});

It will allows you to detect actual size of your control.

Have a nice day and be good people.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • Live
  • Reddit
  • TwitThis
  • email
  • Slashdot
  • StumbleUpon

You may also be interested with:

  1. Quick IT tip: How to build bootable USB stick

5 Responses to “Quick Silverlight tip: Why my ActualWidth and ActualHeight equal 0?”

  1. Community Blogs Says:

    Fons Sonnemans with RollMenu, Frank LaVigne with Community Megaphone SL Map, Martin Mihaylov on LINQ

  2. Dave Says:

    Too bad this doesn’t actually work consistently. I’ve tried this method, SizeChanged, Loaded, LayoutUpdated and they all still report ActualWidth of zero even though the clip is clearly visible.

  3. Tamir Says:

    Try to change priority of invoking call

  4. VBK Says:

    How do you change priority? Does Silverlight 2 allow you to pass a priority to the dispatcher function?

  5. How to Get Six Pack Fast Says:

    After reading this article, I just feel that I really need more info. Could you share some more resources please?

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together