WPF Control DesiredSize has no value

09 Jan

It may be sometimes the case that you create controls via C# code. Especially when you create custom controls. Then you sometimes need the current control size. Well that is easy, may be thought.

Easily call control.DesiredSize.Width and control.DesiredSize.Height. The problem is that you will not get any values by calling Width and Height. This is because the control has not updated the value for the DesiredSize property.

One possible solution is to call the control.Mesure(someVeryBigSize). With that method you call the measurement procedure that calculates and updates the desired size of the control. As parameter you pass the size the control can allocate. If you want to know exactly the size of the control you want to call DesiredSize on you should consider to choose a very big size.

Only after that you have the possibility to retrieve a value for DesiredSize.Height and DesiredSize.Width.

Leave a comment

Posted by on January 9, 2013 in C-Sharp, WPF


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: