如何使用iCustomTypeDescriptor

时间:2019-01-23 10:35:44

标签: list dropdown propertygrid xceed icustomtypedescriptor

我正在使用customTypeDescriptor来用属性填充我的propertygrid。 现在,当一个属性在此属性网格中更改时。我想填充一个由下拉列表中的动态对象,这些动态对象是由先前属性中的更改产生的。 我不能使用枚举,因为这将是简单的解决方案。

我尝试了几种使用不同的customTypeDescriptors和TypeConverters的方法。他们似乎都不起作用。单击here或单击here

我有我的propertyGrid,用于设置SelectedObject:

    Hashtable Something = new Hashtable();
        var col = collection of properties
        foreach (var property in col)
            Something.Add(property.Name, property.value);

        //Add code for dropdown (this does not work)
        string[] array1 = new string[] { "Audi", "BMW", "Mercedes", "Skoda", "Fiat" };
        Something.Add("List of cars", array1);            

        pgrZoneSettings.SelectedObject = null;
        pgrZoneSettings.SelectedObject = new DictionaryPropertyGridAdapter(Something);

DictionaryPropertyGridAdaptor:

public class DictionaryPropertyGridAdapter : ICustomTypeDescriptor
{
    IDictionary _dictionary;

    public DictionaryPropertyGridAdapter(IDictionary d)
    {
        _dictionary = d;
    }

    public string GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }

    public string GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }

    EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return null;
    }

    PropertyDescriptorCollection
        System.ComponentModel.ICustomTypeDescriptor.GetProperties()
    {
        return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        ArrayList properties = new ArrayList();
        foreach (DictionaryEntry e in _dictionary)
        {
            properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key));
        }

        PropertyDescriptor[] props =
            (PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));

        return new PropertyDescriptorCollection(props);
    }


    class DictionaryPropertyDescriptor : PropertyDescriptor
    {
        IDictionary _dictionary;
        object _key;

        internal DictionaryPropertyDescriptor(IDictionary d, object key)
            : base(key.ToString(), null)
        {
            _dictionary = d;
            _key = key;
        }

        public override Type PropertyType
        {
            get { return _dictionary[_key].GetType(); }
        }

        public override void SetValue(object component, object value)
        {
            _dictionary[_key] = value;
        }

        public override object GetValue(object component)
        {
            if (this.PropertyType.IsEnum)
                return _dictionary[_key];
            else
                return _dictionary[_key];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type ComponentType
        {
            get { return null; }
        }

        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override void ResetValue(object component)
        {
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
}

在我的属性列表中,我可以通过一个具有TypeConverter类的枚举来形象化描述:

public class ToolsTypeConverter : EnumConverter
{
    public ToolsTypeConverter(Type type) : base(type)
    {
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType == typeof(string));
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is ToolsForDistribution)
        {
            ToolsForDistribution x = (ToolsForDistribution)value;

            return EnumExtensions.GetDescription(x);
        }
        return base.ConvertFrom(context, culture, value);
    }
}

枚举类:

[TypeConverter(typeof(ToolsTypeConverter))]
public enum ToolsForDistribution
{
    [Description("Test1")]
    test1,
    [Description("Test2")]
    test2
}

所有属性都很好地显示了。枚举也正在提取说明。我只是不知道如何在我的propertiesgrid中获得列表。 我希望具有与枚举相同的下拉列表,但我想在代码中动态更改值,因此请使用列表或其他内容。

0 个答案:

没有答案