2017-04-02 87 views
0

我已经developped光用户控件的一些特性,支持绑定就像那个System.ArgumentException:“无法绑定到属性‘的目标控制值’

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

但whene我试着将它绑定这样的:

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:“无法绑定到属性‘价值’的目标控制

当我调试与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"); 

任何想法的代码?

+0

在方法(DOTNET)CheckBinding(),还有来自我控制IBindableComponent一些铸造,出于这个原因,我的新特性它不到风度存在...私人IBindableComponent控制!; propInfos = TypeDescriptor.GetProperties(control);任何想法!!!! –

回答

0

对不起,我发现了问题,错误的是purly矿,这里是佐比代码

公开新ControlBindingsCollection数据绑定{{返回textBox.DataBindings; }}

对不起

相关问题