yonan2236
yonan2236

Reputation: 13649

Is it possible to change the text being displayed in the PropertyGrid?

Is there a way to change the text ("Item_Type[] Array") being displayed in the PropertyGrid control when the selected object is an Array?

I like how the grid shows each Item in the hierarchy but I think it would be better if I could change or remove the "Item_Type[] Array" text.

enter image description here

Upvotes: 0

Views: 791

Answers (1)

Sinatr
Sinatr

Reputation: 21979

You can achieve that by using custom converter:

public class Test
{
    [TypeConverter(typeof(ConverterArray))]
    public string[] Property { get; set; } = new[] { "1", "2", "3" };
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        propertyGrid1.SelectedObject = new Test();
    }
}

public class ConverterArray : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
    {
        if (destType == typeof(string))
            return "An array kk?";
        return base.ConvertTo(context, culture, value, destType);
    }
}

Screenshot without converter and with:


To see and edit items as expandable list (with indexes as names) inherit converter from ArrayConverter.

If you need to remove/add items you must implement custom editor (usually you would make for this another form and use modal editor UITypeEditorEditStyle.Modal):

public class EditorHeaterPID : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) => UITypeEditorEditStyle.Modal;

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        SomeForm form = new SomeForm(value);
        if (form.ShowDialog() == DialogResult.OK)
            return form.Items;
        return value;
    }
}

Upvotes: 2

Related Questions