[英]How to create custom generic PropertyGrid editor item which opens a form?
[英]How to create custom PropertyGrid editor item which opens a form?
我有一個List <>(我的自定義類)。 我想在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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.