Cat*_*ann 9 .net c# propertygrid uitypeeditor
我想避免在我编写自定义UITypeEditor的某个类型的每个实例上放置EditorAttribute.
我不能在类型上放置EditorAttribute,因为我无法修改源代码.
我引用了将要使用的唯一PropertyGrid实例.
我是否可以告诉PropertyGrid实例(或所有实例)在遇到特定类型时使用自定义UITypeEditor?
这是一篇MSDN文章,它提供了如何在.NET 2.0或更高版本中执行此操作的起点.
Mar*_*ell 19
您通常可以在运行时通过关联编辑器等TypeDescriptor.AddAttributes.例如(Bar属性应显示"...",显示"正在编辑!"):
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
class Foo
{
public Foo() { Bar = new Bar(); }
public Bar Bar { get; set; }
}
class Bar
{
public string Value { get; set; }
}
class BarEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
MessageBox.Show("Editing!");
return base.EditValue(context, provider, value);
}
}
static class Program
{
[STAThread]
static void Main()
{
TypeDescriptor.AddAttributes(typeof(Bar),
new EditorAttribute(typeof(BarEditor), typeof(UITypeEditor)));
Application.EnableVisualStyles();
Application.Run(new Form { Controls = { new PropertyGrid { SelectedObject = new Foo() } } });
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8354 次 |
| 最近记录: |