0

我在使用属性网格集合编辑器时遇到问题,我正在使用自定义类来设置属性等:

public class MetaProp
{
    public MetaProp(string name, Type type, object defaultvalue, params Attribute[] attributes)
    {
        this.Name = name;
        this.Type = type;
        this.DefaultValue = defaultvalue;
        if (attributes != null)
        {
            Attributes = new Attribute[attributes.Length];
            attributes.CopyTo(Attributes, 0);
        }
    }
    public string Name { get; private set; }
    public object DefaultValue { get; private set; }
    public Type Type { get; private set; }
    public Attribute[] Attributes { get; private set; }
}

[TypeConverter(typeof(BasicPropertyBagConverter))]
class BasicPropertyBag
{

    private readonly List<MetaProp> properties = new List<MetaProp>();
    public List<MetaProp> Properties { get { return properties; } }
    private readonly Dictionary<string, object> values = new Dictionary<string, object>();

    public object this[string key]
    {
        get { object value; return values.TryGetValue(key, out value) ? value : null; }
        set { if (value == null) values.Remove(key); else values[key] = value; }
    }

    class BasicPropertyBagConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptor[] metaProps = (from prop in ((BasicPropertyBag)value).Properties
                                              select new PropertyBagDescriptor(prop.Name, prop.Type, prop.DefaultValue, prop.Attributes)).ToArray();
            return new PropertyDescriptorCollection(metaProps);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
        {
            return "(properties)";
        }
    }
    class PropertyBagDescriptor : PropertyDescriptor
    {
        private readonly Type type;
        private readonly object DefaultValue;
        public PropertyBagDescriptor(string name, Type type, object defaultvalue, Attribute[] attributes)
            : base(name, attributes)
        {
            this.type = type;
            this.DefaultValue = defaultvalue;
        }
        public override Type PropertyType { get { return type; } }
        public override object GetValue(object component) { return ((BasicPropertyBag)component)[Name]; }
        public override void SetValue(object component, object value) { ((BasicPropertyBag)component)[Name] = value; }
        public override bool CanResetValue(object component) { return true; }
        public override void ResetValue(object component) { SetValue(component, null); }
        public override bool IsReadOnly { get { return false; } }
        public override Type ComponentType { get { return typeof(BasicPropertyBag); } }
        public override bool ShouldSerializeValue(object component)
        {
            object val = this.GetValue(component);
            if (this.DefaultValue == null && val == null)
                return false;
            else
                return !val.Equals(this.DefaultValue);
        }
    }
}

然后我可以使用如下代码:

var bag = new BasicPropertyBag
{
    Properties = {
        new MetaProp("Number", typeof(int), 5),
        new MetaProp("Items", typeof(List<BasicPropertyBag>), null, new DisplayNameAttribute("Position"))
    }
};
bag["Number"] = 20;
bag["Items"] = new List<BasicPropertyBag>();
propertyGrid1.SelectedObject = bag;

问题是“Items”属性,我需要使用创建的类,如下所示:

var items = new BasicPropertyBag
{
    Properties = {
        new MetaProp("Name", typeof(string), null),
        new MetaProp("Text", typeof(string), null)
    }
};

那么 items 属性将类似于以下内容:

bag["Items"] = new List<items>();

但这显然不起作用,我可以创建自己的类并使其以这种方式工作,但我需要它像这样工作,以及类似的东西,以便集合编辑器正确显示属性。

我不得不发布大部分代码,这样你就可以看到我想要做什么。

4

0 回答 0