更改 PropertyGrid.PropertyValueChanged 时自动展开 属性 不起作用。 (ExpandableObjectConverter 类)

Auto expand Property when changed PropertyGrid.PropertyValueChanged not works. (ExpandableObjectConverter classes)

我需要在设计时扩展一个PropertyGridSelectedItem当我选择 属性 值时。我尝试创建 CollectionEditor 访问 属性 网格的后代。并在覆盖 CreateCollectionForm()

中执行以下操作
    protected override CollectionForm CreateCollectionForm()
    {
        var collectionForm = base.CreateCollectionForm();
        collectionForm.Shown += (s, e) =>
        {
            var propertyGrid = collectionForm.Controls[0].Controls.OfType<PropertyGrid>().First();   

            propertyGrid.PropertyValueChanged += (ss, ee) =>
            {
                if (ee.ChangedItem.Expandable)
                    ee.ChangedItem.Expanded = true;  // NOT WORKING.
            };
        };
        return collectionForm;
    }

另一个人很久以前就问过类似的问题但还没有回答。就我而言,我需要在 winForms 设计器 属性 网格中展开 selectedItem。 Automatically expand some properties in PropertyGrid

我看到他尝试将上下文转换为 GridItem。在我这边崩溃 visual studio.

任何帮助将不胜感激。

为此,必须按如下方式提供 CollectionEditor 后代:

public class AdvancedCollectionEditor : System.ComponentModel.Design.CollectionEditor
{       
    protected internal string formCaption;
    protected internal List<Type> excludedTypes = new List<Type>();
    protected internal bool allowMultiSelect;
    
    public AdvancedCollectionEditor(Type type) : base(type)
    {
        allowMultiSelect = true;
    }

    protected override Type[] CreateNewItemTypes()
    {
        if (CollectionItemType.IsAbstract)
        {
            List<Type> validTypes = new List<Type>();
            var types = Assembly.GetAssembly(CollectionItemType).GetTypes();
            foreach (var type in types)
            {
                if (!type.IsAbstract && type.IsSubclassOf(CollectionItemType))
                {
                    if (!excludedTypes.Contains(type))
                        validTypes.Add(type);
                }
            }
            return validTypes.ToArray();
        }
        return base.CreateNewItemTypes();
    }

    protected override CollectionForm CreateCollectionForm()
    {
        var collectionForm = base.CreateCollectionForm();
        if (!string.IsNullOrWhiteSpace(formCaption))
            collectionForm.Text = formCaption;
        collectionForm.Size = new Size(640, 500);
        collectionForm.Shown += (s, e) =>
        {
            var propertyGrid = collectionForm.Controls[0].Controls.OfType<PropertyGrid>().First();
            propertyGrid.HelpVisible = true;

            propertyGrid.PropertyValueChanged += (ss, ee) =>
            {
               // Note that ee.ChangedItem will never works
                void ExpandChildGridItems(GridItem parent)
                {
                    if (parent.Expandable)
                        parent.Expanded = true;
                    if (parent.GridItems.Count > 0)
                    {
                        foreach (GridItem child in parent.GridItems)
                            ExpandChildGridItems(child);
                    }
                }

                if (propertyGrid.SelectedGridItem.Expandable)
                    propertyGrid.SelectedGridItem.Expanded = true;
                ExpandChildGridItems(propertyGrid.SelectedGridItem);
            };
        };
        return collectionForm;
    }
    protected override bool CanSelectMultipleInstances() => allowMultiSelect;
}