我正在使用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 :(得分:0)
在向PropertyGrid选择对象之前,您可以为Chart类型声明自定义TypeDescriptionProvider:
...
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);
}
}