2012-03-11 4 views
1

나는 이벤트 시스템을 가진 게임에 대한 편집기를 만들려고합니다. 이벤트에 대한 기본 클래스와 실제로 각 종류에 대한 다른 클래스가 있습니다. 모든 것을해라.PropertyGrid에서 객체의 "Value"속성을 표시하는 방법

그래서 PropertyGrid에 표시되는 BaseEvent 목록이 있고 목록으로 컬렉션 편집기가 열립니다. TypeConverter를 준비 했으므로 "Value"속성에 표시된 모든 파생 클래스가있는 드롭 다운이 있습니다.

파생 클래스의 속성이 "값"의 자식으로 표시되지만 BaseEvent에서 속성을 표시하려고하면 "값"속성이 사라지고 루트에 자식이 표시되므로, 이벤트 유형을 변경할 수 없습니다.

"Value"속성을 BaseEvent 속성과 동시에 표시 할 수있는 방법이 있습니까?

//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를 오버라이드 (override)에 구성되어 해결 방법을 발견했습니다 " public new bool BaseProperty {get {return base.BaseProperty;}} "매우 우아하지는 않지만 ... – Osguima3

답변

1

마지막으로 나는이 문제를 해결하는 방법을 발견하십시오 GetProperties를 방법 및 사용자 정의의 PropertyDescriptor를 통해 :

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); 
    } 
} 
관련 문제