属性网格中如何有一个按钮?

发布于 2024-11-17 21:50:58 字数 114 浏览 2 评论 0原文

我有一个属性网格,其中会引用一些属性。我希望属性网格中的其中一个项目是一个按钮,甚至有一个省略号按钮,它的作用就像普通获胜表单上的按钮一样。

有办法做到这一点吗?

提前感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心是晴朗的。 2024-11-24 21:50:58

我建议阅读充分利用 .NET Framework PropertyGrid 控件

它逐步介绍了如何为您的媒体资源创建自定义 UI,其中可能包括一个打开弹出窗口/单独表单/等的按钮。

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.

や三分注定 2024-11-24 21:50:58

我使用扩展方法向 PropertyGrid 添加了全部折叠和展开所有按钮。

PropertyGrid 按钮

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

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));
                }
            }
        }
     }
 }
記憶穿過時間隧道 2024-11-24 21:50:58

UITypeEditor,使用 IWindowsFormsEditorService...就是这样。知道了!感谢您的指导!

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

如日中天 2024-11-24 21:50:58

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

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

    }

用于执行该类的 UIEditor_ListTypeConv_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");
    }
}

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文