如何修改的PropertyGrid在运行时(添加/删除属性和动态类型/枚举) [英] How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums)

查看:446
本文介绍了如何修改的PropertyGrid在运行时(添加/删除属性和动态类型/枚举)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何修改的PropertyGrid在运行时以各种方式?我希望能够添加和删除属性和添加动态类型,我的意思与使用的TypeConverter导致在PropertyGrid中运行时产生的下拉列表中选择类型。

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.

我实际上已经能够做到这两个东西(添加/删除属性和添加动态类型),但只有未单独在同一时间。

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.

要落实支持添加和在运行时删除属性我用这个$ C $的CProject文章,并修改了codeA位,以支持不同类型(不只是字符串)。

To implement the support to add and remove properties at runtime I used this codeproject article and modified the code a bit to support different types (not just 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;
    		}
    	}
    }

等等...

etc...

public enum CaptionPosition
{
    Top,
    Left
}

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

My complete solution can be downloaded here.

它工作正常,当我添加字符串,布尔变量或枚举,但是当我尝试添加一个动态类型之类StatesList这是行不通的。没有人知道为什么,可以帮助我解决呢?

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?

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;
    }
}

使用类型转换器的方法,正常工作时,你不要试图添加属性在运行时,例如的这code 的工作没有任何问题,但我希望能够做到这两点。

The method of using a TypeConverter works fine when you don't try to add the property at runtime, for example this code works without any problem, but I want to be able to do both.

请看看我的项目。 谢谢!

推荐答案

你做了什么,是增加StatesList(一个类型转换器)作为属性。
你应该做的,是增加与StatesList属性作为其类型转换器。

What you do, is adding StatesList (a TypeConverter) as a property.
What you should do, is adding a property with StatesList as its TypeConverter.

这篇关于如何修改的PropertyGrid在运行时(添加/删除属性和动态类型/枚举)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆