Get width of element of auto width

会有一股神秘感。 提交于 2019-12-11 18:41:22

问题


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?


回答1:


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...




回答2:


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.



来源:https://stackoverflow.com/questions/9795986/get-width-of-element-of-auto-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!