开发者

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?

A开发者_StackOverflowppreciate your help in advance!


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.


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));
                }
            }
        }
     }
 }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜