2012-06-24 3 views
2

기본 클래스 (Node)와 상속 된 유형이 있습니다.조건부 속성

Class Node 
{ 
    Base_Attributes... 
} 

Class Derived : Node 
{ 
    Derived_Attributes... 
} 

이러한 유형은 프로젝트에 추가 한 DLL에 있습니다. 그리고 그 클래스의 속성 중 하나가 Node라는 클래스가 있습니다. mainform 어딘가에

Class Item 
{ 
Point location; 
String name; 
Node quiddity; 

bool[] IsBrowsable; 

public Point Location{set;get;} 
public String Name{set;get;} 
public String NodeAttrib{set;get;} 
[Browsable(IsBrowsable[thisindex])] 
public String DerivedTypeAttribe{set;get;} 
[Browsable(IsBrowsable[otheroneindex])] 
public String DerivedTypeAttribe{set;get;} 

Item(string type) 
{ 
    switch(type) 
    { 
     case"some": 
      Node = new derived_some(); 
      IsBrowsable[thisindex] = true; 
      break; 
    } 
} 
} 

과 : :이 같은 itme의 속성을 표시하는 내가 PropertyGrid가이

propertygrid.selectedobject = item; 

문제는 여기에서 파생 된 형식에 지정된 일부 속성이있어 내가 그들을 표시해야 propetygrid하지만 런타임에 노드의 유형을 알 수 없습니다. 나는 부울 배열을 사용하여 Browsabl() 특성을 설정하려고 시도했지만 Browsable Parameter는 상수 값이어야한다고 판명되었습니다. 어떤 생각을 어떻게 할 수 있습니까?

+1

전체적으로 들어갈 시간이 없지만 실제로이 작업을 수행 할 수는 있습니다. TypeConverter 또는 (더 복잡한) ICustomTypeDescriptior. 일반적으로 ExpandableTypeConverter를 상속하고 GetProperties를 재정의 (적절한 경우 필터링)하면 충분합니다. –

+0

답변 해 주셔서 감사합니다. 이걸 어떻게 성취 할 수 있겠 니? – void

답변

1

다음은 TypeDescriptor을 통한 필터링의 예입니다. 물론 "표시 할 속성을 어떻게 알 수 있습니까?"코드를 변경할 수 있습니다.이 코드는 단지 설명의 대상입니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows.Forms; 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     PropertyGrid grid; 
     using (var form = new Form 
     { 
      Controls = { (grid = new PropertyGrid { Dock = DockStyle.Fill}) } 
     }) 
     { 
      grid.SelectedObject = new Magic {ShowY = false}; 
      Application.Run(form); 
     } 
    } 
} 

[TypeConverter(typeof(MagicTypeConverter))] 
public class Magic 
{ 
    public Magic() 
    { 
     ShowX = ShowY = ShowZ = true; 
    } 

    public int A { get; set; } 
    public bool B { get; set; } 
    public string C { get; set; } 
    public int X { get; set; } 
    public bool Y { get; set; } 
    public string Z { get; set; } 

    [Browsable(false)] 
    public bool ShowX { get; set; } 
    [Browsable(false)] 
    public bool ShowY { get; set; } 
    [Browsable(false)] 
    public bool ShowZ { get; set; } 

    private class MagicTypeConverter : ExpandableObjectConverter 
    { 
     public override PropertyDescriptorCollection GetProperties(
      ITypeDescriptorContext context, object value, 
      Attribute[] attributes) 
     { 
      var original = base.GetProperties(context, value, attributes); 
      var list = new List<PropertyDescriptor>(original.Count); 
      var magic = (Magic)value; 
      foreach (PropertyDescriptor prop in original) 
      { 
       bool showProp = true; 
       switch (prop.Name) 
       { 
        case "X": showProp = magic.ShowX; break; 
        case "Y": showProp = magic.ShowY; break; 
        case "Z": showProp = magic.ShowZ; break; 
       } 
       if (showProp) list.Add(prop); 
      } 
      return new PropertyDescriptorCollection(list.ToArray()); 
     } 
    } 
} 
+0

감사합니다. 그것은 정말로 설명이었고 잘 작동했습니다. – void