2012-03-11 63 views
1

イベントの基本クラスを持つゲームのエディタを作成しようとしていますが、実際にはそれぞれの種類の別のクラスすべてのものを行う。PropertyGridのオブジェクトの「値」プロパティを表示する方法

したがって、PropertyGridに表示されるBaseEventのリストがあり、リストとしてコレクションエディタが開きます。私はTypeConverterを用意しましたので、 "Value"プロパティに表示されているすべての派生クラスをドロップダウンしました。

派生クラスのプロパティは、「値」の子として表示されますが、BaseEventからプロパティを表示すると、「値」プロパティが消え、子がルートに表示されるため、イベントのタイプを変更できません。

"Value"プロパティをBaseEventプロパティと同時に表示させる方法はありますか? getPropertiesメソッドを通じて、およびカスタムのPropertyDescriptor:

//This allows to get a dropdown for the derived classes 
public class EventTypeConverter : ExpandableObjectConverter 
{ 
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
    { 
     return new StandardValuesCollection(GetAvailableTypes()); 
    } 

    /* 
    ... 
    */ 
} 

[TypeConverter(typeof(EventTypeConverter))] 
public abstract class BaseEvent 
{ 
    public bool BaseProperty; //{ get; set; } If I set this as a property, "Value" disappears 
} 

public class SomeEvent : BaseEvent 
{ 
    public bool SomeOtherProperty { get; set; } 
} 

//The object selected in the PropertyGrid is of this type 
public class EventManager 
{ 
    public List<BaseEvent> Events { get; set; } //The list that opens the collection editor 
} 
+0

私は "のように、すべての子どものクラスでBasePropertyをオーバーライドに構成され、回避策を見つけましたpublic new bool BaseProperty {get {return base.BaseProperty;}} "あまりエレガントではありませんが... – Osguima3

答えて

1

最後に、私はこの問題を解決する方法を見つけ

public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 
{ 
    //Get the base collection of properties 
    PropertyDescriptorCollection basePdc = base.GetProperties(context, value, attributes); 

    //Create a modifiable copy 
    PropertyDescriptorCollection pdc = new PropertyDesctiptorCollection(null); 
    foreach (PropertyDescriptor descriptor in basePdc) 
     pdc.Add(descriptor); 

    //Probably redundant check to see if the value is of a correct type 
    if (value is BaseEvent) 
     pdc.Add(new FieldDescriptor(typeof(BaseEvent), "BaseProperty")); 
    return pdc; 
} 

public class FieldDescriptor : SimplePropertyDescriptor 
{ 
    //Saves the information of the field we add 
    FieldInfo field; 

    public FieldDescriptor(Type componentType, string propertyName) 
     : base(componentType, propertyName, componentType.GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic).FieldType) 
    { 
     field = componentType.GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic); 
    } 

    public override object GetValue(object component) 
    { 
     return field.GetValue(component); 
    } 

    public override void SetValue(object component, object value) 
    { 
     field.SetValue(component, value); 
    } 
} 
関連する問題