Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...