propertygrid

propertygrid多个对象怎么显示

小亿
169
2023-09-21 20:53:00
栏目: 编程语言

在PropertyGrid中显示多个对象可以通过两种方式实现:

  1. 使用属性类别:

示例代码如下:

public class ParentObject
{
[Category("Group 1")]
public string Property1 { get; set; }
[Category("Group 1")]
public int Property2 { get; set; }
[Category("Group 2")]
public bool Property3 { get; set; }
}
ParentObject parentObject = new ParentObject();
propertyGrid1.SelectedObject = parentObject;
  1. 使用属性描述器:

示例代码如下:

public class CustomTypeDescriptor : ICustomTypeDescriptor
{
private List<object> objects;
public CustomTypeDescriptor(List<object> objects)
{
this.objects = objects;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
// 返回包含多个对象属性的PropertyDescriptorCollection
List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
foreach (var obj in objects)
{
PropertyDescriptorCollection collection = TypeDescriptor.GetProperties(obj, attributes);
foreach (PropertyDescriptor descriptor in collection)
{
properties.Add(new CustomPropertyDescriptor(descriptor, obj));
}
}
return new PropertyDescriptorCollection(properties.ToArray());
}
public PropertyDescriptorCollection GetProperties()
{
return GetProperties(null);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
}
public class CustomPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor descriptor;
private object obj;
public CustomPropertyDescriptor(PropertyDescriptor descriptor, object obj)
: base(descriptor)
{
this.descriptor = descriptor;
this.obj = obj;
}
public override object GetValue(object component)
{
return descriptor.GetValue(obj);
}
public override void SetValue(object component, object value)
{
descriptor.SetValue(obj, value);
}
public override void ResetValue(object component)
{
descriptor.ResetValue(obj);
}
public override bool CanResetValue(object component)
{
return descriptor.CanResetValue(obj);
}
public override bool ShouldSerializeValue(object component)
{
return descriptor.ShouldSerializeValue(obj);
}
public override Type ComponentType
{
get { return descriptor.ComponentType; }
}
public override bool IsReadOnly
{
get { return descriptor.IsReadOnly; }
}
public override Type PropertyType
{
get { return descriptor.PropertyType; }
}
}
List<object> objects = new List<object>
{
new object1(),
new object2(),
new object3()
};
CustomTypeDescriptor typeDescriptor = new CustomTypeDescriptor(objects);
propertyGrid1.SelectedObject = typeDescriptor;

以上两种方式都可以在PropertyGrid中显示多个对象的属性,并根据需要进行分组和组织。

0
看了该问题的人还看了