2012-03-29 19 views
0

我有一個複雜的屬性,我想在PropertyGrid中編輯。PropertyGrid - 收藏版/包裝

interface IInterface{} 

abstract class Base : IInterface{} 

class A : Base{} 
class B : Base{} 

這些類表示可以存儲在屬性中的內容(這些類的內容無關緊要)。

// The Property to be displayed in the PropertyGrid 
class Property 
{ 
    List<Base> MyListOfObjects {get;set;}   
} 

我設法創建一個派生類的System.ComponentModel.Design.CollectionEditor,讓我增添別樣DATAS的,利用集合屬性[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]屬性。

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

    #region Overrides of CollectionEditor 
    protected override Type[] CreateNewItemTypes() 
    { 
     base.CreateNewItemTypes(); 
     // [EDIT assembly, see below] 
     var types = (from t in Assembly.GetAssembly(typeof(IInterface)).GetTypes() 
        where t.GetInterfaces().Contains(typeof (IInterface)) && !t.IsAbstract 
        select t).ToArray(); 
     return types; 
    } 

    protected override Type CreateCollectionItemType() 
    { 
     return typeof(A); // 1st problem 
    } 
} 
  • 第一個問題:我發現的唯一的解決方案,能夠編輯的對象是給一個具體的子類類型CreateCollectionItemType()。爲什麼?如何避免這種情況?

  • 第二個問題:我現在要使用包裝將此屬性賦予propertyGrid項目。我不想在模型中擁有屬性屬性(例如[Category("General")]),而是想將它們放在包裝中。

它適用於一切,但收集。 這是我如何做的:

class abstract WrapperBase<T> 
{ 
    T WrappedObject{get;set;} 
} 
class PropertyWrapper:WrapperBase<Property> 
{ 
    List<Base> MyListOfObjects 
    { 
     get{return WrappedObject.MyListOfObjects;} 
     set{WrappedObject.MyListOfObjects=value;} 
    } 
} 

這樣,集合編輯器不會讓我對象添加到收藏集,這是可用的下拉列表添加特定類型的對象,已經一去不復返了。

有什麼想法?提前致謝!


[編輯]

問題的第二部分得以解決:由於包裝位於另一組件,我不看在IInterface的實現正確的位置。

回答

0

CreateNewItemTypes很好。在CreateCollectionItemType中返回基類型。我認爲這應該工作。

+0

我嘗試過,但PropertyGrid不提供特定於派生類型的字段。 – Benjamin 2012-03-31 17:09:57