c# - PropertyGrid UITypeEditor 禁用单元格编辑

标签 c# propertygrid uitypeeditor

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

但是该属性仍然是可编辑的,这是我不想要的。有没有办法做到这一点?我看了这个类似的问题Propertygrid UIEditor disabling value editing through Keyboard但它并不能解决我的问题,因为解决方案是使用 TypeConverter 的简单下拉列表。

最佳答案

一种解决方案是声明 TypeConverter这并没有什么作用,就像这样:

这是您要编辑的类:

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

这是自定义 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";
    }
}

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

// this class does nothing on purpose
public class MyConverter : TypeConverter
{
}

关于c# - PropertyGrid UITypeEditor 禁用单元格编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194161/

相关文章:

c# - 如何在 C# 的 linq 查询中使用局部变量?

c# - 如何创建无需用户交互即可自行更新的 Windows 服务

c# - WPF 工具包 PropertyGrid ExpandableObject 属性未正确显示嵌套类

c# - PropertyGrid 滚动位置在设置时不改变

.net - 如何在 PropertyGrid 中为模态 UITypeEditor 重新标记省略号按钮?

c# - 我想知道有没有更好的方法来实现这个 "simple lock"

c# - 我怎样才能指示 AutoFixture 不要费心填写一些属性?

C# Propertygrid 属性可为空

c# - 在运行时从自定义 UITypeEditor 中的属性上插入自定义 TypeConverter

vb.net - 如何使自定义控件的属性打开文件对话框?