我遇到属性网格集合编辑器的问题,我使用自定义类来设置属性等:
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;
问题是&#34;项目&#34; property,我需要使用一个创建的类,如下所示:
var items = new BasicPropertyBag
{
Properties = {
new MetaProp("Name", typeof(string), null),
new MetaProp("Text", typeof(string), null)
}
};
那么items属性将是:
bag["Items"] = new List<items>();
但是这显然不起作用,我可以创建自己的类并让它以这种方式工作但我需要它像这样工作,类似的东西,所以集合编辑器将正确显示属性。
我必须发布大部分代码,以便您可以看到我想要做的事情。