How do you have a button in a property grid?
Asked Answered
C

4

5

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!

Cyndy answered 29/6, 2011 at 18:46 Comment(3)
Is this the built-in WinForms PropertyGrid, or something else? (You didn't specify which UI framework you're using.)Glossography
Yes the built in Property Grid. Sorry I thought I had mentioned that.Cyndy
I edited your tags to specify WinForms.Glossography
F
7

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.

Finis answered 29/6, 2011 at 18:47 Comment(0)
P
3

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));
                }
            }
        }
     }
 }
Posthumous answered 20/7, 2016 at 15:49 Comment(0)
C
1

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

Cyndy answered 29/6, 2011 at 20:27 Comment(0)
I
0

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");
    }
}
Intussusception answered 17/5, 2023 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.