首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ICustomTypeDescriptor数据绑定到文本框

ICustomTypeDescriptor数据绑定到文本框
EN

Stack Overflow用户
提问于 2012-02-24 17:30:01
回答 1查看 507关注 0票数 1

我有一个实现ICustomTypeDescriptor的模型。这是因为我希望能够添加自定义属性...,并将它们绑定到文本框。奇怪的是,绑定对PropertyGrid很有效,但对文本框不起作用。

下面的代码可以工作:

代码语言:javascript
复制
DynamicClass<ExtensionModel> binder = new DynamicClass<ExtensionModel>(ext);
propertyGrid1.SelectedObject = binder;

但不是他的:

代码语言:javascript
复制
var binder = new DynamicClass<ExtensionModel>(ext);
_versionLabel.DataBindings.Add("Text", binder, "SelectedVersion", false, DataSourceUpdateMode.OnPropertyChanged);

在这种情况下,我得到的对象与目标类型不匹配。异常。如果我在绑定中放ext而不是绑定器,它工作得很好。

textbox绑定功能有问题吗?

我的DynamicClass代码是:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-02-24 18:28:34

这里有一个基本问题,因为您给出了实际对象(ExtensionModel)的描述符,但组合框只知道包装器(DynamicClass<T>),因此将尝试调用错误对象上的方法。您可以通过将描述符包装在可为您交换对象的内容中来避免这种情况;例如:

代码语言:javascript
复制
class IndirectDescriptor : PropertyDescriptorDecorator
{
    private readonly object instance;
    public IndirectDescriptor(PropertyDescriptor tail, object instance) : base(tail)
    {
        this.instance = instance;
    }
    protected override object GetComponent(object component)
    {
        return instance;
    }
}

abstract class PropertyDescriptorDecorator : PropertyDescriptor
{
    private readonly PropertyDescriptor tail;
    static Attribute[] GetAttributes(AttributeCollection attribs)
    {
        var arr = new Attribute[attribs.Count];
        attribs.CopyTo(arr, 0);
        return arr;
    }
    public PropertyDescriptorDecorator(PropertyDescriptor tail) : base(tail.Name, GetAttributes(tail.Attributes))
    {
        this.tail = tail;
    }
    protected virtual object GetComponent(object component)
    {
        return component;
    }
    public override void AddValueChanged(object component, EventHandler handler)
    {
        tail.AddValueChanged(GetComponent(component), handler);
    }
    public override void RemoveValueChanged(object component, EventHandler handler)
    {
        tail.RemoveValueChanged(GetComponent(component), handler);
    }
    public override bool CanResetValue(object component)
    {
        return tail.CanResetValue(GetComponent(component));
    }
    public override TypeConverter Converter
    {
        get { return tail.Converter; }
    }
    public override string Category
    {
        get { return tail.Category; }
    }
    public override Type ComponentType
    {
        get { return tail.ComponentType; }
    }
    public override string Description
    {
        get { return tail.Description; }
    }
    public override bool DesignTimeOnly
    {
        get { return tail.DesignTimeOnly; }
    }
    public override AttributeCollection Attributes
    {
        get { return tail.Attributes; }
    }
    public override string DisplayName
    {
        get { return tail.DisplayName; }
    }
    public override PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter)
    {
        return tail.GetChildProperties(GetComponent(instance), filter);
    }
    public override object GetEditor(Type editorBaseType)
    {
        return tail.GetEditor(editorBaseType);
    }
    public override object GetValue(object component)
    {
        return tail.GetValue(GetComponent(component));
    }
    public override void SetValue(object component, object value)
    {
        tail.SetValue(GetComponent(component), value);
    }
    public override bool IsBrowsable
    {
        get { return tail.IsBrowsable; }
    }
    public override bool IsLocalizable
    {
        get { return tail.IsLocalizable; }
    }
    public override bool IsReadOnly
    {
        get { return tail.IsReadOnly; }
    }
    public override string Name
    {
        get { return tail.Name; }
    }
    public override Type PropertyType
    {
        get { return tail.PropertyType; }
    }
    public override void ResetValue(object component)
    {
        tail.ResetValue(GetComponent(component));
    }
    public override bool SupportsChangeEvents
    {
        get { return tail.SupportsChangeEvents; }
    }
    public override bool ShouldSerializeValue(object component)
    {
        return tail.ShouldSerializeValue(GetComponent(component));
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9428349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档