양식을 열 수있는 사용자 정의 속성 그리드 편집기 항목을 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1016239

  •  06-07-2019
  •  | 
  •  

문제

목록 <> (내 커스텀 클래스)이 있습니다. 이 목록의 특정 항목을 PropertyGrid 컨트롤의 상자에 표시하고 싶습니다. 상자 끝에서 [...] 버튼을 원합니다. 클릭하면 클릭하면 무엇보다도 목록에서 항목 중 하나를 선택할 수있는 양식이 열립니다. 닫으면 PropertyGrid는 선택한 값을 반영하도록 업데이트됩니다.

모든 도움이 감사합니다.

도움이 되었습니까?

해결책

모달을 구현해야합니다 UITypeEditor, 사용 IWindowsFormsEditorService 표시 서비스 :

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

class MyType
{
    private Foo foo = new Foo();
    public Foo Foo { get { return foo; } }
}

[Editor(typeof(FooEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
class Foo
{
    private string bar;
    public string Bar
    {
        get { return bar; }
        set { bar = value; }
    }
}
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;
        Foo foo = value as Foo;
        if (svc != null && foo != null)
        {            
            using (FooForm form = new FooForm())
            {
                form.Value = foo.Bar;
                if (svc.ShowDialog(form) == DialogResult.OK)
                {
                    foo.Bar = form.Value; // update object
                }
            }
        }
        return value; // 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);
    }
}

참고 : 속성 (부모 개체 등)의 컨텍스트에 대해 액세스 해야하는 경우 이것이 바로 ITypeDescriptorContext (안에 EditValue) 제공; 그것은 당신에게 알려줍니다 PropertyDescriptor 그리고 Instance (그만큼 MyType) 관련이 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top