我有一个带有字符串属性的类,同时具有 getter 和 setter,该属性通常太长,以至于 PropertyGrid 会截断字符串值。如何强制 PropertyGrid 显示省略号,然后启动包含多行文本框的对话框以方便编辑属性?我知道我可能必须在该属性上设置某种属性,但是什么属性以及如何设置?我的对话框是否必须实现一些特殊的设计器界面?
更新: This可能是我的问题的答案,但我无法通过搜索找到它。我的问题比较笼统,它的答案可用于构建任何类型的自定义编辑器。
最佳答案
您需要为该属性设置一个[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;
}
}
您可以通过查看行为符合您要求的现有属性来追踪现有编辑器。
关于.net - 如何强制 PropertyGrid 显示特定属性的自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/359690/