如何判断深层嵌套控件是否处于设计模式

在Winform控件开发过程中,如果3层控件的嵌套关系为A->B->C,则C中的DesignMode属性被识别为运行时而非设计时,这在开发过程中有时是不合适的,可以通过下面的方法正确识别。

/// <summary>
/// 获取一个值,用于指示当前是否处于设计模式。
/// </summary>
public new bool DesignMode
{
get
{
System.Type t
= typeof(System.ComponentModel.Component);
System.Reflection.PropertyInfo pi
= t.GetProperty("DesignMode", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

Control c
= this;
do
{
if ((bool)pi.GetValue(c, null))
{
return true;
}
c
= c.Parent;
}
while (c != null);
return base.DesignMode;

}
}
posted @ 2011-04-21 14:40  thinksea  阅读(253)  评论(0编辑  收藏  举报