问题描述
我有一个带有字符串属性的类,它同时具有 getter 和 setter,通常太长以至于 PropertyGrid 会截断字符串值.如何强制 PropertyGrid 显示省略号,然后启动包含多行文本框的对话框,以便轻松编辑属性?我知道我可能必须在属性上设置某种属性,但是什么属性以及如何设置?我的对话框是否必须实现一些特殊的设计器界面?
I have a class with a string property, having both a getter and a setter, that is often so long that the PropertyGrid truncates the string value. How can I force the PropertyGrid to show an ellipsis and then launch a dialog that contains a multiline textbox for easy editing of the property? I know I probably have to set some kind of attribute on the property, but what attribute and how? Does my dialog have to implement some special designer interface?
更新:这 可能是我的问题的答案,但我找不到它搜索.我的问题比较笼统,它的答案可以用来构建任何类型的自定义编辑器.
Update: This is probably the answer to my question, but I could not find it by searching. My question is more general, and its answer can be used to build any type of custom editor.
推荐答案
你需要为属性设置一个[Editor(...)]
,给它一个UITypeEditor进行编辑的代码>;像这样(使用您自己的编辑器...)
You need to set an for the property, giving it a
UITypeEditor
that does the edit; like so (with your own editor...)
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;
}
}
您可能会通过查看行为符合您需要的现有属性来追踪现有编辑器.
You might be ablt to track down an existing Editor by looking at existing properties that behave like you want.
这篇关于如何强制 PropertyGrid 显示特定属性的自定义对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!