How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums)
Asked Answered
P

4

28

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!

Polio answered 24/11, 2008 at 10:14 Comment(0)
C
8

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

Contrecoup answered 24/11, 2008 at 10:52 Comment(0)
P
6

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.

Polio answered 24/11, 2008 at 12:26 Comment(5)
I know the post is very old, but I have a short question. I user exactly your code within my application but the custom combobox doesn't work. The name of the property is greyed out and no options are available. Any idea, what I can do to fix that? Thanks in advance, MartinBreadth
@user653427, Since this is such an old question, it might also be worth it to post a new question and link back to this page. You'll be able to give more detail and reach more people that way.Bantu
To avoid the extra empty 'States' class you can also return the typeconverter in the PropertyDescriptor implementation (CustomPropertyDescriptor) in your case - the member to override is: public TypeConverter ConverterCopyholder
thank you , if user choose top or bottom from CaptionPosition combobox How can we get that value in the code?Retiary
and another question is when you new a CustomProperty for CaptionPosition enum, you set "top" as a value . but when I run application,at first there isn't "top" value in captionPosition field . when I choose item from that comboBox the related value shown. How can I fix this problem? I mean define default value for comboboxRetiary
M
5

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.

Mum answered 3/8, 2011 at 13:3 Comment(3)
thank you for taking the time to expand on the answer, very helpful.Minutia
what is valueType ? its typeof(list<string>) ?Retiary
It is the type of the property value being passed, which could be something like string, int, or a custom type like States (see @salle55's answer). You could use typeof, like you indicated or perhaps something like value.GetType().Mum
F
0

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;
    }
}
Franke answered 26/4, 2017 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.