PropertyGrid UITypeEditor 禁用单元格编辑

dmc*_*lly 2 c# propertygrid uitypeeditor

我有一个属性网格,其中一个属性使用UITypeEditor来编辑值(在表单上)。

但是,该属性仍然是可编辑的,这是我不想要的。有没有办法做到这一点?我查看了这个类似的问题Propertygrid UIEditor 通过键盘禁用值编辑,但它没有解决我的问题,因为解决方案是使用 TypeConverter 的简单下拉列表。

Sim*_*ier 5

一种解决方案是声明一个TypeConverter,它什么都不做,就像这样:

这是您要编辑的类:

public class MyClass
{
    [Editor(typeof(MyClassEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(MyConverter))]
    public string MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是自定义 UITypeEditor:

public class MyClassEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        MessageBox.Show("press ok to continue");
        return "You can't edit this";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我花了几天时间编写的著名转换器:

// this class does nothing on purpose
public class MyConverter : TypeConverter
{
}
Run Code Online (Sandbox Code Playgroud)