2011-08-06 5 views
7

Visual StudioのようなエディタをPropertyGridの文字列に使用する最も簡単な方法は何ですか?例えばAutos/Locals/Watchesでは文字列の値をプレビュー/編集することができますが、虫眼鏡をクリックして外部のウィンドウに文字列を表示することもできます。C#プロパティグリッド文字列エディタ

+0

あなたがあなた自身のUITypeEditorで*この一部*を行うことができます。代わりに初期のアプリでどこかに、以下を適用し、[Editor(...)]を追加します。 –

答えて

8

UITypeEditorでこれを行うことができます。ここで私は、個々のプロパティでそれを使用していますが、(あなたはすべてのプロパティを飾るために必要がないように)IIRCあなたはまた、すべての文字列を覆すことができます。

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

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     using(var frm = new Form { Controls = { new PropertyGrid { 
      Dock = DockStyle.Fill, SelectedObject = new Foo { Bar = "abc"}}}}) 
     { 
      Application.Run(frm); 
     } 
    } 
} 

class Foo 
{ 
    [Editor(typeof(FancyStringEditor), typeof(UITypeEditor))] 
    public string Bar { get; set; } 
} 
class FancyStringEditor : UITypeEditor 
{ 
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
    { 
     return UITypeEditorEditStyle.Modal; 
    } 
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
    { 
     var svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); 
     if (svc != null) 
     { 
      using (var frm = new Form { Text = "Your editor here"}) 
      using (var txt = new TextBox { Text = (string)value, Dock = DockStyle.Fill, Multiline = true }) 
      using (var ok = new Button { Text = "OK", Dock = DockStyle.Bottom }) 
      { 
       frm.Controls.Add(txt); 
       frm.Controls.Add(ok); 
       frm.AcceptButton = ok; 
       ok.DialogResult = DialogResult.OK; 
       if (svc.ShowDialog(frm) == DialogResult.OK) 
       { 
        value = txt.Text; 
       } 
      } 
     } 
     return value; 
    } 
} 

すべてのためにこれを適用するには文字列のメンバー:

TypeDescriptor.AddAttributes(typeof(string), new EditorAttribute(
    typeof(FancyStringEditor), typeof(UITypeEditor))); 
+0

私は実際にあなたの答えをdownvoteする必要があります。なぜなら、私は怠け者になり、コードをコピー&ペーストするだけだからです。 ;) – John

+0

はい、このコードは完璧でシンプルですぐに動作します。ありがとう – IEnumerable