How to enable Default Values for properties in a 'CollectionEditor' dialog

前端 未结 1 643
时光说笑
时光说笑 2021-01-22 05:14

Please read entire question first to understand where I would have the ability to reset the default value of a property.

When defining a custom

相关标签:
1条回答
  • 2021-01-22 05:42

    You can create your own collection editor inheriting CollectionEditor class and then override CreateCollectionForm method, find property grid in the collection editor form and then register a ContextMenuStrip containing a Reset menu item for property grid, then reset the property using ResetSelectedProperty:

    public class MyCollectionEditor : CollectionEditor
    {
        public MyCollectionEditor() : base(typeof(Collection<MyElement>)) { }
        protected override CollectionForm CreateCollectionForm()
        {
            var form = base.CreateCollectionForm();
            var grid = form.Controls.Find("propertyBrowser", true).First() as PropertyGrid;
            var menu = new ContextMenuStrip();
            menu.Items.Add("Reset", null, (s, e) => { grid.ResetSelectedProperty(); });
            //Enable or disable Reset menu based on selected property
            menu.Opening += (s, e) =>
            {
                if (grid.SelectedGridItem != null && grid.SelectedObject != null &&
                    grid.SelectedGridItem.PropertyDescriptor.CanResetValue(null))
                    menu.Items[0].Enabled = true;
                else
                    menu.Items[0].Enabled = false;
            };
            grid.ContextMenuStrip = menu;
            return form;
        }
    }
    

    And decorate your collection property this way:

    [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Collection<MyElement> MyElements { get; private set; }
    

    Following this approach you can simply add a separator, commands and description menus.

    0 讨论(0)
提交回复
热议问题