Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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", WorkStatus, "Done");

I can confirm the binding is successfully added to the control's databindings list but if I change my bound object (WorkStatus), nothing happens.

This is what WorkStatus looks like:

public class WorkStatus : INotifyPropertyChanged
{
    private Boolean _done;
    public Boolean Done
    {
        get { return _done; }

        set
        {
            if (_done == value) return;

            _done = value;

            // fire event
            RaisePropertyChanged("Done");
        }
    }

    private Int32 _time;
    public Int32 Time
    {
        get { return _time; }

        set
        {
            if (_time == value) return;

            _time = value;

            // fire event
            RaisePropertyChanged("Time");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(String propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null) { PropertyChanged(this, e); }
    }
}

Edit
To reproduce, just set the Visible=false in the designer, or in the constructor before the databinding.
Using one overload of the Add() method fails too:

this.checkBox_WorkDone.DataBindings.Add("Visible", WorkStatus, "Done",
   true, DataSourceUpdateMode.OnPropertyChanged);

The reason I want to hide the control is that I don't want user to see the control when the form is shown the very first time.

Solution
Thanks guys, I think I find a solution for this:

just set the Control.Visible = false in the Form.Load() event. In that case the control is not visible when the form is shown.

Although, why MS design the data binding in this way is still unknown.

like image 623
AZ. Avatar asked Mar 22 '12 18:03

AZ.


2 Answers

What you could do is make the control visible, and make it invisible again once the binding has changed.

this.checkBox_WorkDone.Visible = true; 
this.checkBox_WorkDone.BindingContextChanged += (object sender, EventArgs e) => {
    this.checkBox_WorkDone.Visible = false; 
};

Not very pretty, but it works.

like image 161
Bas Avatar answered Oct 06 '22 23:10

Bas


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.

like image 38
Scott Chamberlain Avatar answered Oct 06 '22 23:10

Scott Chamberlain