如何获得Visible属性的"真实"价值?

Qwe*_*tie 11 .net visible winforms

如果将Windows窗体控件的Visible属性设置为true,则如果隐藏任何控件的父窗口,该属性仍将返回false.有没有办法在隐藏父窗口的情况下获取控件的真实底层可见性标志?

Mar*_*ell 13

好吧,常规实现检查控件堆栈,以确保所有父项都可见.我知道躲避这一切的唯一方法是用反思欺骗,并要求GetState(2),但这是脆弱的:

    // dodgy; not recommended
    Panel query;
    Form form = new Form
    {
        Controls = {
            new Panel {
                Visible = false,
                Controls = {
                    (query = new Panel {Visible = true})
                }
            }
        }
    };
    form.Show();

    // this is the dodgy bit...
    bool visible = (bool)typeof(Control)
        .GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic)
        .Invoke(query, new object[] { 2 });
Run Code Online (Sandbox Code Playgroud)