如何在运行时修改PropertyGrid(添加/删除属性和动态类型/枚举)

如何在运行时以各种方式修改propertygrid? 我希望能够添加和删除属性并添加“动态类型”,我的意思是使用TypeConverter在propertygrid中生成运行时生成的下拉列表。

我实际上已经能够做这两件事(添加/删除属性和添加动态类型)但不能同时单独使用。

为了实现在运行时添加和删除属性的支持,我使用了这个代码项目文章并稍微修改了代码以支持不同的类型(不仅仅是字符串)。

private System.Windows.Forms.PropertyGrid propertyGrid1; private CustomClass myProperties = new CustomClass(); public Form1() { InitializeComponent(); myProperties.Add(new CustomProperty("Name", "Sven", typeof(string), false, true)); myProperties.Add(new CustomProperty("MyBool", "True", typeof(bool), false, true)); myProperties.Add(new CustomProperty("CaptionPosition", "Top", typeof(CaptionPosition), false, true)); myProperties.Add(new CustomProperty("Custom", "", typeof(StatesList), false, true)); //<-- doesn't work } ///  /// CustomClass (Which is binding to property grid) ///  public class CustomClass: CollectionBase,ICustomTypeDescriptor { ///  /// Add CustomProperty to Collectionbase List ///  ///  public void Add(CustomProperty Value) { base.List.Add(Value); } ///  /// Remove item from List ///  ///  public void Remove(string Name) { foreach(CustomProperty prop in base.List) { if(prop.Name == Name) { base.List.Remove(prop); return; } } } 

等等…

 public enum CaptionPosition { Top, Left } 

我的完整解决方案可以在这里下载。

当我添加字符串,bool或枚举时,它工作正常,但当我尝试添加像StatesList这样的“动态类型”时它不起作用。 有谁知道为什么,可以帮助我解决它?

 public class StatesList : System.ComponentModel.StringConverter { private string[] _States = { "Alabama", "Alaska", "Arizona", "Arkansas" }; public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(_States); } public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } } 

当您不尝试在运行时添加属性时,使用TypeConverter的方法可以正常工作,例如, 此代码可以正常工作,但我希望能够同时执行这两个操作。

请看看我的项目 。 谢谢!

你做的是将StatesList(一个TypeConverter)添加为属性。
你应该做的是添加一个StatesList作为TypeConverter的属性。

啊,当然!

 myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true)); [TypeConverter(typeof(StatesList))] public class States { } 

像魅力一样,谢谢!

我已经更新了我的项目,希望它对其他人有帮助,可以在这里找到。

这个问题和答案对我来说非常有用。 但是,我需要通过允许运行时生成的下拉列表值来进一步扩展。 我想我会发布一些关于它需要的示例代码,以防有人发现它有用。

首先,我向CustomProperty构造函数添加了一个options参数,并添加了一个Options属性:

  private List lOptions; public CustomProperty(string sName, object value, Type tType, bool bReadOnly, bool bVisible, List lOptions) { this.lOptions = lOptions; } public List Options { get { return lOptions; } } 

其次,我向CustomPropertyDescriptor类添加了一个Options属性:

  public List Options { get { return m_Property.Options; } } 

第三,我必须修改动态类型类(即StatesList)中的GetStandardValues方法,以利用CustomPropertyDescriptor对象上的新Options属性:

  public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { CustomPropertyDescriptor descriptor = (CustomPropertyDescriptor)context.PropertyDescriptor; return new StandardValuesCollection(descriptor.Options); } 

最后,我必须在创建新的CustomProperty对象时传入我的选项列表:

  List optionsList = new List(new string[] { "test1", "test2", "test3" }); CustomProperty myProperty = new CustomProperty(attr.Name, attr.Value, valueType, false, true, optionsList); 

代替我为此示例传入的静态列表,您可以以任何方式生成下拉列表的选项列表,从而完全控制可用选项。

在我的情况下,TypeConverter不适用于States类

 [TypeConverter(typeof(StatesList))] // not work public class States { } 

所以我在CustomPropertyDescriptor中添加了覆盖

 public override TypeConverter Converter { get { if (this.PropertyType.Equals(typeof(States)) ) { return new StatesList(); ; } return base.Converter; } }