如何在属性网格中添加一个按钮?

5

我有一个属性网格,其中将引用几个属性。我希望属性网格中的一个项目是一个按钮,或者甚至有一个省略号按钮,它将像普通win表单上的按钮一样起作用。

有没有办法做到这一点?

提前感谢您的帮助!


1
这是内置的WinForms PropertyGrid,还是其他什么东西?(您没有指定使用哪个UI框架。) - Joe White
是的,内置属性网格。抱歉,我以为我已经提到了。 - Sean P
我已经编辑了你的标签,以指定WinForms。 - Joe White
4个回答

7

2
我使用扩展方法为PropertyGrid添加了“全部折叠”和“全部展开”按钮。 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));
                }
            }
        }
     }
 }

1

UITypeEditor,使用IWindowsFormsEditorService...就是这样。明白了!感谢您的指引!


0

为了执行它,您可以创建一个类,例如名为my_Button的类,其中不包含任何项,并在该类的顶部定义以下属性:

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

    }

UIEditor_List 类用于执行该类,TypeConv_List 是它的名称,这些值可以定义如下:

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

在你想要成为按钮的项目类的最后一个foreach属性中,定义如下。
public my_Button Read_Mode { get; set; } = new my_Button();

propertygrid中定义事件名称propertyGrid1_PropertyValueChanged并定义您想要进行的每个更改,如下所示:
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");
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接