【发布时间】: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