page hit counter
Quick Silverlight tip: Why my ActualWidth and ActualHeight equal 0? - Just code - Tamir Khason
Saturday, July 12, 2008 11:21 AM Tamir Khason

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

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/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.

תגים:, , ,

Comments

No Comments