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

问题描述

在使用C#4.0 / C#2.0中,我不能绑定到一个控制,如果控制的可见场是假的WinForms:

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 =在设计假的,或者在数据绑定前的构造结果
使用添加一个过载()方法也失败了:

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:

刚刚设置的Control.Visible =在Form.Load()事件假的。在这种情况下显示窗体时,该控件是不可见的。

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(真)。然而,该方法是一个受保护的方法,所以你必须这样做,虽然反射或通过扩展控制。

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



所有的事件将被推迟,直到控制了创建设置为true。

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

11-03 06:01