当 Control.Visible == false 时无法将数据绑定到控件 [英] Cannot data bind to a control when Control.Visible == false

查看:10
本文介绍了当 Control.Visible == false 时无法将数据绑定到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有 C# 4.0/C# 2.0 的 WinForms 中,如果控件的可见字段为 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");

我可以确认绑定已成功添加到控件的数据绑定列表中,但是如果我更改绑定对象 (WorkStatus),则什么也不会发生.

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

WorkStatus 是这样的:

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); }
    }
}

编辑
要重现,只需在设计器中或在数据绑定之前的构造函数中设置 Visible=false.
使用 Add() 方法的一个重载也会失败:

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:

只需在 Form.Load() 事件中设置 Control.Visible = false.在这种情况下,显示窗体时控件不可见.

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

尽管如此,MS 为何以这种方式设计数据绑定仍然未知.

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

推荐答案

我遇到了 之前的确切情况.在控件第一次可行之前,一些后端初始化永远不会发生,初始化的一部分是启用数据绑定.您必须在数据绑定工作之前调用 CreateControl(true).但是,该方法是受保护的方法,因此您必须通过反射或扩展控件来实现.

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.

通过反射:

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 } );
}

所有事件将被推迟,直到控件将 Created 设置为 true.

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

这篇关于当 Control.Visible == false 时无法将数据绑定到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆