2009-06-19 28 views
26

< Listem var (özel sınıfım). Bu listede belirli bir öğeyi PropertyGrid denetimindeki bir kutuda görüntülemek istiyorum. Kutunun sonunda [...] düğmesini istiyorum. Tıklandığında, diğer şeylerin yanı sıra, Listedeki öğelerden birini seçmelerine izin veren bir form açılır. Kapatıldığında, PropertyGrid seçilen değeri yansıtacak şekilde güncellenecektir.Bir form açan özel PropertyGrid düzenleyicisi öğesi nasıl oluşturulur?

Herhangi bir yardım için teşekkür ederiz.

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

Not:

cevap

60

ile görüntülemeye IWindowsFormsEditorService servisini kullanarak, bir kalıcı UITypeEditor uygulamak gerekir mülk (üst nesne vs.) kapsamında hakkında bir şey erişmeniz gerekiyorsa, yani ITypeDescriptorContext (EditValue'da) ne sağlar; ilgili olan PropertyDescriptor ve Instance (MyType) bildirir.

+3

Keşke bunu iki kere + 1 yapabilseydim! Ben neredeyse 2 yıl önce geçmişte bıraktım ve farklı bir projede yine aynı soruya geri döndüm ve hala ihtiyacım olan şeyi buldum. Harika cevap, teşekkürler! – Paccc

+0

Her şey güzel görünüyor, ancak FooEditor hiçbir yerde kullanılmıyor. Bazı özellikler için TypeEditor olarak ayarlanmalıdır. – virious

+1

@virious type-editors özelliği veya özelliği karşı belirtilebilir; bu durumda ('Editör (typeo (FooEditor), typeof (UITypeEditor))]' 'ile' Foo 'türüne uygulanır. Bu nedenle *** foo türündeki her *** özelliği varsayılan olarak bu düzenleyiciyi kullanır. Örnekte “MyType.Foo” ifadesine bakın. –

İlgili konular