WPF中,如果一个对象的宽度是auto,那么在构造的时候去得到ActualWidth,结果是0或者非数字,那是因为构造的时候layout部分还没有建立起来.解决方法有二个.
A. layoutUpdated事件,可以放在这个里面,但是不好维护,还需要移除layoutUpdated事件的侦听,要不然会发生逐帧运算,很耗资源
B.用这个方法,写在构造函数中
this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
{
Debug.WriteLine("My Acrual Width"+this.ActualWidth);
[...]
在第一时间得到AcrualWidth
本文探讨了WPF中当对象宽度设置为Auto时,ActualWidth返回0或非数字的问题,并提供了两种解决方案:监听layoutUpdated事件和使用Dispatcher.BeginInvoke方法。

被折叠的 条评论
为什么被折叠?



