C#

时间:2017-05-23 10:35:31

标签: c# propertygrid

我正在尝试在我的项目中使用属性网格,请找到我的源代码 -

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

class MyType
{
    private prop1 objprop1 = new prop1();
    private prop2 objprop2 = new prop2();
    public prop1 prop1 { get { return objprop1; } }
    public prop2 prop2 { get { return objprop2; } }
}

[Editor(typeof(FooEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
class prop1
{

}

[Editor(typeof(FooEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
class prop2
{

}

class FooEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
        string newvalue="";

        if (svc != null)
        {
            using (FooForm form = new FooForm())
            {
               // form.Value = foo.Value1;
                if (svc.ShowDialog(form) == DialogResult.OK)
                {
                    newvalue = form.Value; // update object
                }
            }
        }
        return (object)newvalue; // can also replace the wrapper object here
    }
}

class FooForm : Form
{
    private TextBox textbox;
    private Button okButton;
    public FooForm()
    {
        textbox = new TextBox();
        Controls.Add(textbox);
        okButton = new Button();
        okButton.Text = "OK";
        okButton.Dock = DockStyle.Bottom;
        okButton.DialogResult = DialogResult.OK;
        Controls.Add(okButton);
    }
    public string Value
    {
        get { return textbox.Text; }
        set { textbox.Text = value; }
    }
}

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Form form = new Form();
        PropertyGrid grid = new PropertyGrid();
        grid.Dock = DockStyle.Fill;
        form.Controls.Add(grid);
        grid.SelectedObject = new MyType();
        Application.Run(form);
    }
}

例如,如果我输入" pr1value.pr2value"在文本框中然后在属性网格值应存储 - prop1 = pr1value, prop2 = pr2value 属性可以超过2个它的样本应用程序。

我能够获得我在文本框中输入的值,直到EditValue事件,但无法在属性网格中设置。

0 个答案:

没有答案