tags:

views:

108

answers:

3

hi, I want to get a UIElement's Size, but when I use the DesiredSize or RenderSize property, it always return {0,0}. Could anyone help me? Thank you!

+1  A: 

Take a look at the element's ActualWidth and ActualHeight properties. They'll only have values after the element has been rendered.

AndrewS
A: 

Thank you! I found that if I require size in constructor like following DragDrop : Window { public DragDrop() { InitializeComponent();

        //require size here
    }
}

I cann't get it no matter which properties I ask. But if I ask it in a PreviewKeyDown event handler or something like this, I can get it. But I think the element has been rendered after calling InitializeComponent. Am I wrong? Has the element been rendered when I ask them in the constructor?

I tried to reproduce your problem to no avail, could you post the code you are actually using?
Kiranu
A: 

Your problem happens because while inside the constructor, you do not have rendered elements. The elements are initialized yes, but they have not been rendered yet, therefore the value returned when you call ActualHeight or ActualWidth is 0 and Height and Width is NaN (Not a Number).

If you ask for the same information after the constructor has finished (for example during the Loaded event for the Window) you will receive that information.

Kiranu