How to show Drop down control in Property Grid?
Asked Answered
I

1

15

I am adding the Property grid control in my project. I have to show the Drop down box in one field of Property Grid. Is there any solution to apply this.

Intertwist answered 1/7, 2014 at 6:8 Comment(3)
Use Template Field and ItemTemplate in your GridView.Salami
No, That's not grid. That is PropertyGrid control in windows form.Intertwist
This link might help you more than the accepted answer -> codeproject.com/Articles/23242/… Although I think it might be a slight overkill to use a ListBox, I think just a good olde in-memory List would do the jobYvetteyvon
N
21

You have to declare a type editor for the property in your PropertyGrid and then add to the list of choices. This example creates a Type Converter and then overrides the GetStandardValues() method to provide choices to the drop-down:

private String _formatString = null;
[Category("Display")]
[DisplayName("Format String")]
[Description("Format string governing display of data values.")]
[DefaultValue("")]
[TypeConverter(typeof(FormatStringConverter))]
public String FormatString { get { return _formatString; } set { _formatString = value; } }

public class FormatStringConverter : StringConverter
{
    public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
    public override Boolean GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; }
    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        List<String> list = new List<String>();
        list.Add("");                      
        list.Add("Currency");              
        list.Add("Scientific Notation");   
        list.Add("General Number");        
        list.Add("Number");                
        list.Add("Percent");               
        list.Add("Time");
        list.Add("Date");
        return new StandardValuesCollection(list);
    }
}

The key is the property being assigned a Type Converter in the line:

[TypeConverter(typeof(FormatStringConverter))]

That provides you with the opportunity to introduce your own behavior via overrides.

Here's a simpler example, which allows the Enum type of a Property to automatically provide its values to the PropertyGrid drop-down:

public enum SummaryOptions
{
    Sum = 1,
    Avg,
    Max,
    Min,
    Count,
    Formula,
    GMean,
    StdDev
}

private SummaryOptions _sumType = SummaryOptions.Sum;
[Category("Summary Values Type")]
[DisplayName("Summary Type")]
[Description("The summary option to be used in calculating each value.")]
[DefaultValue(SummaryOptions.Sum)]
public SummaryOptions SumType { get { return _sumType; } set { _sumType = value; } }

By virtue of the fact that the property is an Enum type, those enum values pass through to become the drop-down options automatically.

Norikonorina answered 1/7, 2014 at 11:39 Comment(2)
using this, how can I show dynamic elements in the dropdown? how can i pass a custom list to FormatStringConverter for different properties?Decentralize
You might want to post that as a new question. That's probably more involved that you realize. If you truly mean "dynamic", the biggest issue might be refreshing it as you want changes. This is more of a "grab it once" kind of list, not a list that should be ever-changing.Norikonorina

© 2022 - 2024 — McMap. All rights reserved.