views:

1073

answers:

1

I want to be able to show in a propertygrid a dropdownlist that show some "string" value but return an "int" value.

For example, let set I got this class :

public class MyObjectOptions : StringConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        MyObjectCollection mm = new MyObjectCollection();

        List<String> names = new List<String>
        foreach (MyObject m in mm)
        {
            m.Id // Need to store this somewhere ...
            names.Add(m.Name);
        }

        return new StandardValuesCollection(name);
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;
    }
}

Here is my class use in the propertygrid control

public class MyObjectProperty
{
    [TypeConverter(typeof(MyObjectOptions))]
    public int Id
    {
        get { return this.id; }
        set { this.id = value; }
    }
}

Like you can see, I want to store the id of the object, but I want to show it's name in the dropdownlist ... I try use a hashtable but it's doesn't work ...

BTW - I use the version 3.5 of .Net but I only use WinForm (not WPF).

+1  A: 

You can not use GetStandardValues for that. This method would be useful if you had to restrict your integer values to let's say 1, 5 and 10.

If you want to show strings in your property value, you just need to override the ConvertTo and ConvertFrom methods of your TypeConverter. The PropertyGrid will use the converted strings in the dropdown list.

About your hashtable, you can store it in your TypeConverter if its content is static. If it's dynamic, the best is to let the target instance of the grid manage it. From your converter, you will be able to access it through the TypeDescriptorContext.Instance property.

Hope that helps.

Nicolas Cadilhac