Binding System.ArgumentException:';无法绑定到属性';价值';论目标控制
我开发了light UserControl,其中的一些属性支持这样的绑定Binding System.ArgumentException:';无法绑定到属性';价值';论目标控制,binding,user-controls,Binding,User Controls,我开发了light UserControl,其中的一些属性支持这样的绑定 public class EditDateTimeBox : UserControl,IKeyboardNavigation { private TextBox textBox; private MonthCalendar monthCalendar = new MonthCalendar(); ToolStripDropDown popup = new ToolStripDropDown();
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()方法中的绑定类(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");
//如果控件被继承,则获取
//控件的类型,而不是控件本身。得到
//控件的属性将控件的属性与
//它的设计者。正常情况下,我们希望这样,但为了
//我们不需要继承控件,因为继承控件应该
//像运行时控件一样“操作”。
//
InheritanceAttribute attr=(InheritanceAttribute)TypeDescriptor.GetAttributes(控件)[typeof(InheritanceAttribute)];
if(attr!=null&&attr.InheritanceLevel!=InheritanceLevel.NotInherited){
PropInfo=TypeDescriptor.GetProperties(controlClass);
}
否则{
PropInfo=TypeDescriptor.GetProperties(控件);
}
for(int i=0;i
有什么想法吗?对不起,我发现了问题,错误完全是我的,这是僵尸代码 public new controlbindings集合数据绑定{get{return textBox.DataBindings;}} 抱歉在方法(dotnet)CheckBinding()中,有一些从我的控件到IBindableComponent的转换,因此,我的新属性不存在!…私有IBindableComponent控件;PropInfo=TypeDescriptor.GetProperties(控件);有什么想法吗!!!!