Sean P
Sean P

Reputation: 949

How do you have a button in a property grid?

I have a property grid that will have a few properties referenced. I would like to have one of the items in the property grid to be a button or even have a ellipses button which will act like a button on a normal win form.

Is there a way to do this?

Appreciate your help in advance!

Upvotes: 5

Views: 11181

Answers (4)

aria
aria

Reputation: 1

To perform it you can create a class like a name my_Button without any item in it and at the top of this class define attribute like this:

[Editor(typeof(UIEditor_List), typeof(UITypeEditor)), TypeConverter(typeof(TypeConv_List)), Serializable]
public class my_Button
    {

    }

UIEditor_List class for performing of the class and TypeConv_List is the name of it and these value can define as below:

public class TypeConv_List : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            return "Click ...";
        }
    }

public class UIEditor_List : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            my_Button list = value as my_Button;
            list = new my_Button();
            return list;
        }
    }

and at the last foreach property of your item class that want to be button define like this

public my_Button Read_Mode { get; set; } = new my_Button();

and in the propertygrid define event name propertyGrid1_PropertyValueChanged and define every change you want to do like below:

private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
    if (propertyGrid1.SelectedGridItem.Label == nameof(filr_dir.Read_Mode))
    {
        System.Diagnostics.Process.Start("explorer.exe", "notpad.txt");
    }
}

Upvotes: 0

Roman Bystrianyk
Roman Bystrianyk

Reputation: 31

I added collapse all and expand all buttons to the PropertyGrid using extension methods.

PropertyGrid Buttons

namespace MyNameSpace
{

    public static class PropertyGridHelper
    {

        private static PropertyGrid getPropertyGridParent(object sender)
        {
            PropertyGrid propertyGrid = null;
            ToolStripButton toolStripButton = sender as ToolStripButton;

            // ToolStripButton -> ToolStrip -> PropertyGrid
            if (toolStripButton != null)
            {
                ToolStrip toolStrip = toolStripButton.GetCurrentParent() as ToolStrip;

                if (toolStrip != null)
                {
                    propertyGrid = toolStrip.Parent as PropertyGrid;

                    if (propertyGrid != null)
                    {
                        propertyGrid.CollapseAllGridItems();
                    }
                }
            }  
            return propertyGrid;
        }

        private static void propertyGridCollapseAllClick(object sender, EventArgs e)
        {
            PropertyGrid propertyGrid = getPropertyGridParent(sender);

            if (propertyGrid != null)
            {
                propertyGrid.CollapseAllGridItems();
            }         
        }

        private static void propertyGridExpandAllClick(object sender, EventArgs e)
        {
            PropertyGrid propertyGrid = getPropertyGridParent(sender);

            if (propertyGrid != null)
            {
                propertyGrid.ExpandAllGridItems();
            }
        }

        public static void AddCollapseExpandAllButtons(this System.Windows.Forms.PropertyGrid propertyGrid)
        {

            foreach (Control control in propertyGrid.Controls)
            {
                ToolStrip toolStrip = control as ToolStrip;

                if (toolStrip != null)
                {
                    toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.CollapseAll, propertyGridCollapseAllClick));
                    toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.ExpandAll, propertyGridExpandAllClick));
                }
            }
        }
     }
 }

Upvotes: 3

Sean P
Sean P

Reputation: 949

UITypeEditor, using the IWindowsFormsEditorService... thats what it was. Got it! Thanks for the direction!

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564671

I recommend reading Getting the Most Out of the .NET Framework PropertyGrid Control.

It walks through how to create a custom UI for your property, which could include a button that opens a popup/separate form/etc.

Upvotes: 7

Related Questions