如何修改PropertyGrid在运行时间(增加/删除的财产和动态型/枚举)
-
10-07-2019 - |
题
你怎么修改propertygrid在运行时,在每一个方法吗?我希望能够增加和删除性质和添加"动态的类型",我的意思是这是一种类型,导致运行时所产生的下拉菜单propertygrid使用。.
我实际上能做到这两点的那些东西(增加/删除性质和加入动态的类型),但唯独不在同一时间。
实施支持增加和删除性质在运行时我用的 这个演示文章 和修改的代码一位以支持不同类型(不仅仅是strings)。
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;
}
}
}
等等...
public enum CaptionPosition
{
Top,
Left
}
我的完整的解决方案,可以下载 在这里,.
它的工作现在我请加串、弹球或枚举,但是当我试图添加"动态型"等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;
}
}
该方法的使用。正常工作时不要试图添加的属性在运行时,例如 这个代码 工作没有任何问题,但我希望能够做到这两点。
请看看 我的项目.谢谢!
解决方案
你做什么,是增加StatesList(一类型转换器)作为属性。点击 你应该做的,是增加与StatesList属性作为其类型转换器。
其他提示
啊,当然!
myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true));
[TypeConverter(typeof(StatesList))]
public class States
{
}
就像一个魅力,谢谢!
我已经更新了我的项目,希望它可以帮助别人,可以发现这里
这个问题的答案是伟大的实用性给我。不过,我需要通过允许运行时产生的下拉列表中值位进一步延伸的东西。我以为我会发布的问候一些示例代码什么需要它,如果任何人发现它是有用的。
首先,我添加了一个选项参数到CustomProperty的构造,并增加了选项属性:
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; }
}
其次,我添加了一个选项属性到CustomPropertyDescriptor类:
public List<string> Options
{
get
{
return m_Property.Options;
}
}
第三,我不得不修改GetStandardValues方法在我的动态型类(即StatesList)利用上CustomPropertyDescriptor对象的新选项属性的:
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
CustomPropertyDescriptor descriptor = (CustomPropertyDescriptor)context.PropertyDescriptor;
return new StandardValuesCollection(descriptor.Options);
}
最后,我在我的选项列表通过创建一个新的CustomProperty的对象时:
List<string> optionsList = new List<string>(new string[] { "test1", "test2", "test3" });
CustomProperty myProperty = new CustomProperty(attr.Name, attr.Value, valueType, false, true, optionsList);
在地方,我通过在这个例子中,静态列表中,您可以生成的选项列表以任何方式请您,让您对可用的选项完全控制你的下拉菜单。
在我的案件类型转换器并不适用于美国一流
[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;
}
}