获取自动宽度元素的宽度 [英] Get width of element of auto width

查看:40
本文介绍了获取自动宽度元素的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 StackPanelWidth.我试过了:

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;

给出 NaN 因为宽度之前设置为 auto .那么我怎样才能得到宽度呢?

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

推荐答案

可以使用sizeChanged,但是当你使用的堆栈面板不改变大小时它不起作用.

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;
}

希望能帮到你...

这篇关于获取自动宽度元素的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆