C# 如何在运行时修改 PropertyGrid(添加/删除属性和动态类型/枚举)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/313822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums)
提问by salle55
How do you modify a propertygrid at runtime in every way? I want to be able to add and remove properties and add "dynamic types", what I mean with that is a type that result in a runtime generated dropdown in the propertygrid using a TypeConverter.
您如何在运行时以各种方式修改属性网格?我希望能够添加和删除属性并添加“动态类型”,我的意思是一种使用 TypeConverter 在 propertygrid 中生成运行时生成下拉列表的类型。
I have actually been able to do both those things (add/remove properties and add dynamic type) but only separately not at the same time.
我实际上已经能够做这两件事(添加/删除属性和添加动态类型),但只是分开而不是同时。
To implement the support to add and remove properties at runtime I used this codeproject articleand modified the code a bit to support different types (not just strings).
为了实现在运行时添加和删除属性的支持,我使用了这篇 codeproject 文章并稍微修改了代码以支持不同的类型(不仅仅是字符串)。
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
}
/// <summary>
/// CustomClass (Which is binding to property grid)
/// </summary>
public class CustomClass: CollectionBase,ICustomTypeDescriptor
{
/// <summary>
/// Add CustomProperty to Collectionbase List
/// </summary>
/// <param name="Value"></param>
public void Add(CustomProperty Value)
{
base.List.Add(Value);
}
/// <summary>
/// Remove item from List
/// </summary>
/// <param name="Name"></param>
public void Remove(string Name)
{
foreach(CustomProperty prop in base.List)
{
if(prop.Name == Name)
{
base.List.Remove(prop);
return;
}
}
}
etc...
等等...
public enum CaptionPosition
{
Top,
Left
}
My complete solution can be downloaded here.
我的完整解决方案可以在这里下载。
It works fine when I add strings, bools or enums, but when I try to add a "dynamic type" like StatesList it doesn't work. Does anyone know why and can help me to solve it?
当我添加字符串、布尔值或枚举时,它工作正常,但是当我尝试添加“动态类型”(如 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;
}
}
The method of using a TypeConverter works fine when you don't try to add the property at runtime, for example this codeworks without any problem, but I want to be able to do both.
当您不尝试在运行时添加属性时,使用 TypeConverter 的方法可以正常工作,例如此代码可以正常工作,但我希望能够同时执行这两项操作。
Please take a look at my project. Thanks!
请看看我的项目。谢谢!
采纳答案by Vincent Van Den Berghe
What you do, is adding StatesList (a TypeConverter) as a property.
What you should do, is adding a property with StatesList as its TypeConverter.
你所做的是添加 StatesList(一个 TypeConverter)作为一个属性。
您应该做的是添加一个以 StatesList 为 TypeConverter 的属性。
回答by salle55
Ah, of course!
啊,当然!
myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true));
[TypeConverter(typeof(StatesList))]
public class States
{
}
Works like a charm, thank you!
像魅力一样工作,谢谢!
I have updated my project, hope it can be helpful to others, it can be found here.
我已经更新了我的项目,希望它可以对其他人有所帮助,可以在这里找到。
回答by Scott
This question and answer was of great usefulness to me. However, I needed to extend things a bit further by allowing for run-time generated dropdown list values. I thought I would post some sample code in regards to what it required, in case anyone finds it useful.
这个问题和答案对我很有用。但是,我需要通过允许运行时生成的下拉列表值来进一步扩展。我想我会发布一些关于它需要什么的示例代码,以防有人觉得它有用。
First, I added an options parameter to the CustomProperty constructor and added an Options property:
首先,我向 CustomProperty 构造函数添加了一个 options 参数并添加了一个 Options 属性:
private List<string> lOptions;
public CustomProperty(string sName, object value, Type tType, bool bReadOnly, bool bVisible, List<string> lOptions)
{
this.lOptions = lOptions;
}
public List<string> Options
{
get { return lOptions; }
}
Second, I added an Options property to the CustomPropertyDescriptor class:
其次,我向 CustomPropertyDescriptor 类添加了一个 Options 属性:
public List<string> Options
{
get
{
return m_Property.Options;
}
}
Third, I had to modify the GetStandardValues method in my dynamic type class (i.e. StatesList) to make use of the new Options property on the CustomPropertyDescriptor Object:
第三,我必须修改动态类型类(即 StatesList)中的 GetStandardValues 方法,以利用 CustomPropertyDescriptor 对象上的新选项属性:
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
CustomPropertyDescriptor descriptor = (CustomPropertyDescriptor)context.PropertyDescriptor;
return new StandardValuesCollection(descriptor.Options);
}
Finally, I had to pass in my list of options when creating a new CustomProperty object:
最后,在创建新的 CustomProperty 对象时,我必须传入我的选项列表:
List<string> optionsList = new List<string>(new string[] { "test1", "test2", "test3" });
CustomProperty myProperty = new CustomProperty(attr.Name, attr.Value, valueType, false, true, optionsList);
In place of the static list that I passed in for this example, you can generate the list of options for your dropdown in any manner that you please, giving you full control over the options available.
代替我在此示例中传入的静态列表,您可以按照您喜欢的任何方式为下拉列表生成选项列表,让您完全控制可用选项。
回答by Dizz Y
in my case TypeConverter did not apply to States class
在我的情况下 TypeConverter 不适用于 States 类
[TypeConverter(typeof(StatesList))] // not work
public class States
{
}
so i added override in CustomPropertyDescriptor
所以我在 CustomPropertyDescriptor 中添加了覆盖
public override TypeConverter Converter
{
get {
if (this.PropertyType.Equals(typeof(States)) ) {
return new StatesList(); ;
}
return base.Converter;
}
}