【问题标题】:System.ArgumentException: 'Cannot bind to the property 'Value' on the target controlSystem.ArgumentException:'无法绑定到目标控件上的属性'Value'
【发布时间】:2017-04-02 21:12:28
【问题描述】:

我已经开发了轻量级的 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");

有什么想法吗?

【问题讨论】:

  • 在方法(dotnet)CheckBinding()中,有一些从我的控件到IBindableComponent的转换,因此,我的新属性它不存在!...私有IBindableComponent控件; propInfos = TypeDescriptor.GetProperties(control);任何想法!!!!

标签: binding user-controls


【解决方案1】:

对不起,我发现了问题,错误完全是我的,这是僵尸代码

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

对不起

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    相关资源
    最近更新 更多