2008-12-11 87 views
11

我有一个字符串属性的类,有一个getter和setter,这通常是这样长,PropertyGrid截断字符串值。如何强制PropertyGrid显示省略号,然后启动包含多行文本框的对话框以便轻松编辑该属性?我知道我可能必须在属性上设置某种属性,但是属性和方式如何?我的对话框是否必须实现一些特殊的设计器界面?如何强制PropertyGrid显示特定属性的自定义对话框?

更新: This可能是我的问题的答案,但我无法通过搜索找到它。我的问题更一般,其答案可以用来构建任何类型的自定义编辑器。

回答

17

您需要为该属性设置一个[Editor(...)],给它一个UITypeEditor进行编辑;像这样(用你自己的编辑器...)

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


static class Program 
{ 
    static void Main() 
    { 
     Application.Run(new Form { Controls = { new PropertyGrid { SelectedObject = new Foo() } } }); 
    } 
} 



class Foo 
{ 
    [Editor(typeof(StringEditor), typeof(UITypeEditor))] 
    public string Bar { get; set; } 
} 

class StringEditor : UITypeEditor 
{ 
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
    { 
     return UITypeEditorEditStyle.Modal; 
    } 
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
    { 
     IWindowsFormsEditorService svc = (IWindowsFormsEditorService) 
      provider.GetService(typeof(IWindowsFormsEditorService)); 
     if (svc != null) 
     { 
      svc.ShowDialog(new Form()); 
      // update etc 
     } 
     return value; 
    } 
} 

你可能会ABLT追查通过观察其行为像你想现有的属性现有的编辑器。

+1

感谢您的快速回答。我现在给你一个+1,一旦有机会在我的最后尝试,我会将其标记为正确答案。 – flipdoubt 2008-12-11 15:49:46