2010-12-20 2 views
0

특정 참조로 CollectionEditor로 만든 새 개체를 초기화해야합니다.특정 참조로 .NET CollectionEditor에서 개체 초기화 됨

특히 PropertyGrid에서 편집 할 수있는 파이프 라인 개체가 있습니다. 이 개체에는 마커 모음이 들어 있습니다. 마커는 계산을하기 위해 파이프 라인에 대한 참조가 필요합니다.

현재 파이프 라인 용 PropertyGrid에는 마커에 대한 항목이 있습니다. 타원형 버튼을 클릭하면 CollectionEditor가 나타납니다. 프로퍼티 편집은 괜찮지 만 새로 생성 된 모든 마커에 대해 현재 파이프 라인을 설정해야합니다. 나는 그 일을하는 가장 좋은 방법이 확실치 않습니다. 모니터링 할 수있는 이벤트가 있습니까? 커스텀 CollectionEditor를 생성해야합니까 (그러나 특정 파이프 라인에 대해 어떻게 알 수 있습니까?).

답변

1

사용자 지정 CollectionEditor 및 사용자 지정 PropertyDescriptor 클래스를 만들어야합니다. PropertyDescriptor는 PropertyDescriptor.GetEditor를 재정 의하여 콜렉션 편집기로 전달되는 PipeLine 객체를 저장할 수 있습니다. PipeLine이 새 Markers 객체를 만들고 필요한 초기화를 수행하도록 할 수 있습니다.

public class MyCollectionEditor : System.ComponentModel.Design.CollectionEditor 
{ 
private Pipeline _pipeline; 

    public MyCollectionEditor(Type type) : base(type) {} 

    public MyCollectionEditor(Type type, Pipeline pipeline) : base(type) 
    { 
     _pipeline = pipeline; 
    } 

    protected override object CreateInstance(Type itemType) 
    { 
     return _pipeline.CreateNewMarker(); 
    } 
} 

public class MyPropertyDescriptor : PropertyDescriptor 
{ 
private PipeLine _pipeline; 

public MyPropertyDescriptor(PipeLine pipeline) : base(name, null) 
{ 
    _pipeline = pipeline; 
} 

public override object GetEditor(Type editorBaseType) 
{ 
    return new MyCollectionEditor(typeof(MarkerCollection), _pipeline); 
} 

// ... other overrides ... 

} 

// ... 
// Implement System.ComponentModel.ICustomTypeDescriptor.GetProperties 

public System.ComponentModel.PropertyDescriptorCollection GetProperties() 
{ 
PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null); 
foreach (Marker m in Markers) { 
    MyPropertyDescriptor pd = new MyPropertyDescriptor(m); 
    pdc.Add(pd); 
} 
return pdc; 
} 
+0

어떻게 당신이의 PropertyDescriptor를 할당 않습니다

여기에 당신이 시작하는 몇 가지 코드는? 할당 할 수있는 속성이 있습니까? 아니면 CustomDesigner가 필요합니까? – doobop

+0

ICustomTypeDescriptor의 GetProperties 메서드 구현에 MyPropertyDescriptor 개체를 할당합니다. 내 대답에 몇 가지 코드를 추가했습니다. – Peladao