プロパティグリッドで複数のオブジェクトを表示するには?
PropertyGridで複数のオブジェクトを表示する方法には2種類あります。
- 属性カテゴリを使用して
- 子オブジェクトを含んだ親オブジェクトを作成します。
- 各サブオブジェクトに属性カテゴリを追加して、PropertyGrid にてグループを別々に表示する。
- PropertyGridのSelectedObjectプロパティに親オブジェクトを設定します。
サンプルコードは以下の通りです。
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;
- 属性記述子を使用する
- ICustomTypeDescriptorインターフェイスを継承したカスタムな属性記述子クラスを作成し、関連メソッドを実装する。
- GetProperties()メソッドでは、オブジェクトのプロパティに関する情報を複数プロパティ記述子として返します。
- インスタンス化されたプロパティの記述情報を、PropertyGrid の SelectedObject プロパティに設定する。
例のコードは次の通りだ:
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;
上記2つの方法はPropertyGridで複数のオブジェクトのプロパティを表示したり、必要に応じてグループ化、整理することができます。