System.ArgumentException:'无法绑定到目标控件上的属性'Value'

时间:2017-04-02 21:12:28

标签: binding user-controls

我已开发出轻型UserControl,其中包含一些支持绑定的属性

public class EditDateTimeBox : UserControl,IKeyboardNavigation
{

    private TextBox textBox;

    private MonthCalendar monthCalendar = new MonthCalendar();
    ToolStripDropDown popup = new ToolStripDropDown();
    ToolStripControlHost host;
   //.....
   [Bindable(true)]
    public DateTime Value{get;set;}
 }

但是当我尝试像这样绑定它时:

    bool _binded = false;
    string _dataColumn ;
    [Category("OverB")]
    [Browsable(true)]
    public string DataColumn
    {
        get
        {
            return _dataColumn;

        }
        set
        {
            _dataColumn = value;
            if (!_binded && !string.IsNullOrEmpty(_dataColumn) && _dataSource != null)
            {
                this.DataBindings.Add("Value", DataSource, _dataColumn,true);
                _binded = true;
            }
        }
    }

抛出一个错误说: System.ArgumentException:'无法绑定到目标控件上的属性'Value'

当我使用dotnet支持调试时,我发现ChechBinding()方法中的Binding类(System.Windows.Forms)会导致问题 这是带注释的代码

// If the control is being inherited, then get the properties for
            // the control's type rather than for the control itself.  Getting
            // properties for the control will merge the control's properties with
            // those of its designer.  Normally we want that, but for 
            // inherited controls we don't because an inherited control should 
            // "act" like a runtime control.
            //
            InheritanceAttribute attr = (InheritanceAttribute)TypeDescriptor.GetAttributes(control)[typeof(InheritanceAttribute)];
            if (attr != null && attr.InheritanceLevel != InheritanceLevel.NotInherited) {
                propInfos = TypeDescriptor.GetProperties(controlClass);
            }
            else {
                propInfos = TypeDescriptor.GetProperties(control);
            }

            for (int i = 0; i < propInfos.Count; i++) {
                if(tempPropInfo==null && String.Equals (propInfos[i].Name, propertyName, StringComparison.OrdinalIgnoreCase)) {
                    tempPropInfo = propInfos[i];
                    if (tempPropIsNullInfo != null)
                        break;
                }
                if(tempPropIsNullInfo == null && String.Equals (propInfos[i].Name, propertyNameIsNull, StringComparison.OrdinalIgnoreCase)) {
                    tempPropIsNullInfo = propInfos[i];
                    if (tempPropInfo != null)
                        break;
                }
            }

            if (tempPropInfo == null) {
                throw new ArgumentException(SR.GetString(SR.ListBindingBindProperty, propertyName), "PropertyName");

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

抱歉,我发现了问题,错误是我的,这里是zomby代码

public new ControlBindingsCollection DataBindings {get {return textBox.DataBindings; }}

抱歉