代码之家  ›  专栏  ›  技术社区  ›  flipdoubt

如何强制PropertyGrid显示特定属性的自定义对话框?

  •  11
  • flipdoubt  · 技术社区  · 17 年前

    我有一个具有string属性的类,它同时具有getter和setter,这通常很长,以至于propertygrid会截断字符串值。如何强制PropertyGrid显示省略号,然后启动包含多行文本框的对话框,以便轻松编辑属性?我知道我可能需要在属性上设置某种属性,但是什么属性以及如何设置?我的对话框必须实现一些特殊的设计器接口吗?

    更新: This 可能是我问题的答案,但我找不到。我的问题更一般,它的答案可以用于构建任何类型的自定义编辑器。

    1 回复  |  直到 16 年前
        1
  •  17
  •   Marc Gravell    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;
        }
    }
    

    您可能无法通过查看按您希望的方式工作的现有属性来跟踪现有的编辑器。