Automatically expand some properties in PropertyGrid

Viewed 217

I would like to automatically expand some nodes in a PropertyGrid loaded with an instance of my SettingsStructure class by using attributes on the properties of that class. Also, I am attempting to have the instance 'remember' whether each property was expanded or not if the user loads that instance again on the PropertyGrid.

I have made a real HACK that mostly works. It does not always work if the complex property is the first one displayed in the PropertyGrid.

Any suggestions for a better way using attributes/typeconverters/similar?

Here is what I have:

enter image description here

Define a custom Attribute to denote that the property it marks is to start expanded.

[AttributeUsage(AttributeTargets.Property)]
public class StartExpanded : Attribute {}

Derive your own class from ExpandableObjectConverter.

public class MyExpandableObjectConverter : ExpandableObjectConverter
{
    private bool _IsFirstUse = true;
    private bool _JustShownInPropertyGrid = false;
    private bool _WasLastExpanded = false;

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        //This method is called every time the propertygrid shows this property
        if (_IsFirstUse)
        {
            _IsFirstUse = false;
            _WasLastExpanded = context.PropertyDescriptor.Attributes[typeof(StartExpanded)] != null;
        }
        _JustShownInPropertyGrid = true;
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        //This method is called after CanConvertFrom and also on other operations and when leaving showing this property
        if (_JustShownInPropertyGrid)
        {
            _JustShownInPropertyGrid = false;
            if (_WasLastExpanded)
            {
                var GI = (GridItem)context;
                GI.Expanded = true;
            }
        }
        else
        {
            var GI = (GridItem)context;
            _WasLastExpanded = GI.Expanded;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

And now apply them to properties in your class to be shown in the PropertyGrid. ASettingsNode is just an abstract class that I use to mark properties that should load in the TreeView on the left.

[Serializable]
public class SettingsStructure  : ASettingsNode
{
    public string FamilyName { get; set; } = "Rogers";
    
    [StartExpanded]
    [TypeConverter(typeof(MyExpandableObjectConverter))]
    public NameAgePair Dad { get; set; } = new NameAgePair() { Name = "Buck", Age = 51};
    
    [StartExpanded]
    [TypeConverter(typeof(MyExpandableObjectConverter))]
    public NameAgePair Mom { get; set; } = new NameAgePair() { Name = "Wilma", Age = 50};
    
    public string NameOfSomebody { get; set; } = "Phoebe";
    //... and other nodes that are derived from ASettingsNode to show up in the TreeView
}

Here NameAgePair is my class.

public class NameAgePair
{
    public string Name { get; set; } = "";
    public int Age { get; set; } = 0;

    public override string ToString()
    {
        return $"{Name} ({Age})";
    }
}

Besides being a HACK, it does not work if the first item in the grid is one of the complex properties that I want to expand and remember. For that first property the ConvertTo method is called out of sequence and the "remember" part fails.

1 Answers

There is no direct solution for setting initial expanded states in the MSPG. Only hacks. And, as you can see, trying to do it in a TypeConverter is not easy. Furthermore, there is no built-in way to save and restore various states of the grid.

As the developer of a custom PropertyGrid made from scratch, I tried to achieve both of your requests. And it definitively works with a few lines of code only. If a commercial product is an option, you can have a look.

Related