Shibli
Shibli

Reputation: 6149

Get width of element of auto width

I am trying to get the Width of a StackPanel. I tried:

double width = stk_main.ActualWidth;

which gave zero and it shouldnt be. Also:

double width = stk_main.Width;

which gives NaN because width set to auto previously. So how can I get the width?

Upvotes: 0

Views: 1815

Answers (2)

Dave S
Dave S

Reputation: 775

You could attach to the Loaded event of the StackPanel as the layout pass will have completed by this point and the ActualWidth will be set.

Upvotes: 0

user1531714
user1531714

Reputation: 111

You can use sizeChanged, but it doesn't work when the stack panel you used not change size.

private void stk_main_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double yourWidthNow = e.NewSize.Width;
}

Hope it can give you some help...

Upvotes: 1

Related Questions