2016-07-17 15 views
0

我使用C#屬性網格添加新的對象和更改特定對象的設置。我需要知道如何使用組件模型將一個變量傳遞給構造函數。原因爲什麼是因爲需要參數才能正確定義圖表對象的初始值。C#屬性網格通行證構造可變

List<Chart> charts = new List<Chart>(); 
[Description("Charts")] 
[Category("4. Collection Charts")] 
[DisplayName("Charts")] 
public List<Chart> _charts 
{ 
    get { return charts; } 
    set { charts = value ; } 
} 




public class Chart 
{ 
    public static string collectionName = ""; 

    int chartPosition = GetMaxChartIndex(collectionName); 
    [Description("Chart posiion in document")] 
    [Category("Control Chart Settings")] 
    [DisplayName("Chart Position")] 
    public int _chartPosition 
    { 
     get { return chartPosition; } 
     set { chartPosition = value; } 
    } 


    public Chart(string _collectionName) 
    { 
     collectionName = _collectionName; 

    } 
} 

回答

0

你可以做什麼是聲明的自定義TypeDescriptionProvider的圖表類型,您選擇對象之前提前進入的PropertyGrid:

... 
TypeDescriptor.AddProvider(new ChartDescriptionProvider(), typeof(Chart)); 
... 

這裏是自定義提供(你需要實施CreateInstance方法):

public class ChartDescriptionProvider : TypeDescriptionProvider 
{ 
    private static TypeDescriptionProvider _baseProvider = TypeDescriptor.GetProvider(typeof(Chart)); 

    public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args) 
    { 
     // TODO: implement this 
     return new Chart(...); 
    } 

    public override IDictionary GetCache(object instance) 
    { 
     return _baseProvider.GetCache(instance); 
    } 

    public override ICustomTypeDescriptor GetExtendedTypeDescriptor(object instance) 
    { 
     return _baseProvider.GetExtendedTypeDescriptor(instance); 
    } 

    public override string GetFullComponentName(object component) 
    { 
     return _baseProvider.GetFullComponentName(component); 
    } 

    public override Type GetReflectionType(Type objectType, object instance) 
    { 
     return _baseProvider.GetReflectionType(objectType, instance); 
    } 

    public override Type GetRuntimeType(Type reflectionType) 
    { 
     return _baseProvider.GetRuntimeType(reflectionType); 
    } 

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) 
    { 
     return _baseProvider.GetTypeDescriptor(objectType, instance); 
    } 

    public override bool IsSupportedType(Type type) 
    { 
     return _baseProvider.IsSupportedType(type); 
    } 
} 
+0

不應該'_baseprovider不是靜態的或者在靜態構造函數中設置? – pinkfloydx33

+0

@ pinkfloydx33 - 爲什麼不,它只是一個樣本 –

+0

感謝張貼。我不確定這是否會有所幫助。我不明白「TypeDescriptor.AddProvider」部分。所有我需要做的是將參數傳遞給圖表類的構造函數時,屬性網格創建一個新的圖表對象 – user1035217