propertygrid的问题 [英] Problem with propertygrid

查看:95
本文介绍了propertygrid的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我如何将类似ComboBox控件的Items属性添加到我的属性网格中?



我的意思是我希望有一个带有浏览按钮的文本框作为propertygrid的属性,当点击按钮时会打开一个表单。



我尝试过建议的解决方案这里,但问题是我不想拥有expandableObject.Infact我想在FooProperty前面显示FooForm的结果而不是Bar属性的值。

Hi all,

How can i add something like "Items" property of ComboBox control to my propertygrid?

I mean i want to have a textbox with a browse button next to it as a property of propertygrid, that a form opens when the button will be clicked.

I have tried the solution suggested here, But the problem is that i do not want to have expandableObject.Infact i want to show the result of FooForm just in front of FooProperty not as the value of Bar property.

推荐答案

你好M_Moghrabbi,



看看这个可运行的例子:

很难从你的描述中看出是否这样正是你想要的 - 但我认为它非常接近:



Hi M_Moghrabbi,

Have a look at this runnable example:
Hard to tell from your description if this is exactly what you want - but I think it's quite close:

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropertyGridTest
{
    static class Program
    {
        class TestPropertyEditor : UITypeEditor
        {
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }

            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                IWindowsFormsEditorService editorservice = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
                if (editorservice != null)
                {
                    // Let's create a test dialog to select some strings
                    // - Replace this whith your selection dialog form and your objects
                    Form formDialog = new Form();
                    Button buttonOk = new Button { Text = "Ok", Dock = DockStyle.Bottom, DialogResult = System.Windows.Forms.DialogResult.OK, Parent = formDialog };
                    Button buttonCancel = new Button { Text = "Cancel", Dock = DockStyle.Bottom, DialogResult = System.Windows.Forms.DialogResult.Cancel, Parent = formDialog };
                    formDialog.CancelButton = buttonCancel;
                    formDialog.AcceptButton = buttonOk;
                    ListBox listbox = new ListBox { Dock = DockStyle.Fill, Parent = formDialog };
                    // Add some test items to select
                    listbox.Items.AddRange(new object[] { "Test1", "Test2", "Test3" });

                    // re-select current value
                    if (value != null)
                        listbox.SelectedItem = value;

                    // show the dialog
                    if (editorservice.ShowDialog(formDialog) == DialogResult.OK)
                    {
                        // set the selected object as new value (which is returned)
                        value = listbox.SelectedItem;
                    }

                }
                return value;
            }
        }

        class TestObject
        {
            // The Property with the special editor
            [Editor(typeof(TestPropertyEditor), typeof(UITypeEditor))]
            public string TestProperty { get; set; }
        }

        static void Main()
        {
            // Create a simple test Form with a PropertyGrid
            Form formMain = new Form();
            PropertyGrid pg = new PropertyGrid { Dock = DockStyle.Fill, Parent = formMain };
            pg.SelectedObject = new TestObject();

            Application.Run(formMain);
        }
    }
}





所以我创建了一个 TestObject 表示您在PropertyGrid上显示的对象类型的类。这个类有一个字符串属性 TestProperty - 这表示你的PropertyGrid中的TextBox值 - 但我们设置了编辑器我们自己的编辑器的属性( TestPropertyEditor )。如果单击(initally empty)属性值旁边的按钮,我们的编辑器实现中定义的对话框将打开并显示选择。



此代码是什么你要? - 如果没有随意请求进一步的帮助......



亲切的问候



Johannes



So I created a TestObject class representing the object type you show on your PropertyGrid. This class has one string property TestProperty - this represents the "TextBox" value in your PropertyGrid - but we set the Editor Attribute to our own editor (TestPropertyEditor). If you click on the button next to the (initally empty) property value, the dialog defined inside our Editor implementation will open and show a selection.

Does this code what you want? - if not feel free to ask for further help...

Kind regards

Johannes


使用收集类:



私有xItems()作为新列表([MyClass])

公共物业项目()作为列表([MyClass])

获取:

返回xItems



Set :(值作为列表([MyClass]))

xItems =价值



结束财产
Using Collection Class:

Private xItems() As New List (Of [MyClass])
Public Property Items() As List (Of [MyClass])
Get:
Return xItems

Set: (Value As List (Of [MyClass]))
xItems = Value

End Property


这篇关于propertygrid的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆