1
我有一個模型實現ICustomTypeDescriptor。這是因爲我希望能夠添加不在對象中的自定義屬性並將它們綁定到文本框。奇怪的部分是綁定對PropertyGrid很好,但對文本框不起作用。ICustomTypeDescriptor數據綁定到文本框
此代碼:
DynamicClass<ExtensionModel> binder = new DynamicClass<ExtensionModel>(ext);
propertyGrid1.SelectedObject = binder;
,但不是他的一個:
var binder = new DynamicClass<ExtensionModel>(ext);
_versionLabel.DataBindings.Add("Text", binder, "SelectedVersion", false, DataSourceUpdateMode.OnPropertyChanged);
在這種情況下,我得到的對象不匹配目標類型。例外。如果我把ext而不是綁定在綁定它工作正常。
是否存在文本框綁定功能的問題?
我對DynamicClass代碼:
public class DynamicClass<T> : ICustomTypeDescriptor
{
private readonly T _object;
public DynamicClass(T trackedObject)
{
_object = trackedObject;
}
// Collection to code add dynamic properties
public KeyedCollection<string, DynamicProperty> Properties { get; private set; }
// ICustomTypeDescriptor implementation
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(_object, true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(_object, true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(_object, true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(_object, true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(_object, true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(_object, true);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(_object, editorBaseType, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(_object, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(_object, attributes, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(_object, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(_object, attributes, true);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return _object;
}
}
我們可以看到您的任何自定義描述符嗎? (特別是對於SelectedVersion)通常這應該工作正常(我做了很多瘋狂的自定義描述符的東西),所以我的直覺說:代碼中的錯誤你也沒有顯示 – 2012-02-24 09:37:14
也提到「文本框」,但你似乎綁定到ComboBox ...? – 2012-02-24 09:38:36
我現在將添加代碼。對不起,錯誤的複製/粘貼與綁定:( – 2012-02-24 09:42:40