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

发布于 2024-07-09 18:53:14 字数 2884 浏览 11 评论 0原文

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

实际上,我已经能够完成这两件事(添加/删除属性和添加动态类型),但只能单独完成,而不是同时完成。

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

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

当您不尝试在运行时添加属性时,使用 TypeConverter 的方法效果很好,例如 这段代码工作没有任何问题,但我希望能够做到这两点。

请查看我的项目。 谢谢!

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.

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.

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

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.

Please take a look at my project.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情洁癖 2024-07-16 18:53:14

您所做的是将 StatesList (类型转换器)添加为属性。
您应该做的是添加一个以 StatesList 作为其 TypeConverter 的属性。

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

云归处 2024-07-16 18:53:14

啊,当然!

myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true));

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

就像魅力一样,谢谢!

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

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.

ま昔日黯然 2024-07-16 18:53:14

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

首先,我向 CustomProperty 构造函数添加了一个选项参数,并添加了一个 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; }
    }

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

    public List<string> Options
    {
        get
        {
            return m_Property.Options;
        }
    }

第三,我必须修改动态类型类(即 StatesList)中的 GetStandardValues 方法才能使用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);

您可以生成选项列表,以代替我为此示例传入的静态列表您可以按照您喜欢的任何方式进行下拉菜单,让您完全控制可用的选项。

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:

    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:

    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:

    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:

    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.

再见回来 2024-07-16 18:53:14

就我而言,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;
    }
}

in my case TypeConverter did not apply to States class

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

so i added override in CustomPropertyDescriptor

public override TypeConverter Converter
{
    get {
        if (this.PropertyType.Equals(typeof(States)) ) {
            return new StatesList(); ; 
        }
        return base.Converter;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文