Cannot data bind to a control when Control.Visible == false

后端 未结 7 645
独厮守ぢ
独厮守ぢ 2020-12-14 11:22

In WinForms with C# 4.0 / C# 2.0, I cannot bind to a control if the control\'s visible field is false:

this.checkBox_WorkDone.DataBindings.Add(\"Visible\", W         


        
相关标签:
7条回答
  • 2020-12-14 12:25

    I ran in to this exact situation before. Until the control is viable for the first time some back-end initialization never happens, part of that initialization is enabling the data binding. You must call CreateControl(true) before data binding works. However, that method it is a protected method so you must do it though reflection or by extending the control.

    Via reflection:

    private static void CreateControl( Control control )
    {
        var method = control.GetType().GetMethod( "CreateControl", BindingFlags.Instance | BindingFlags.NonPublic );
        var parameters = method.GetParameters();
        Debug.Assert( parameters.Length == 1, "Looking only for the method with a single parameter" );
        Debug.Assert( parameters[0].ParameterType == typeof ( bool ), "Single parameter is not of type boolean" );
    
        method.Invoke( control, new object[] { true } );
    }
    

    All events will be deferred until the control has Created set to true.

    0 讨论(0)
提交回复
热议问题