【问题标题】:PropertyGrid UITypeEditor Disable cell editPropertyGrid UITypeEditor 禁用单元格编辑
【发布时间】:2015-05-12 14:40:24
【问题描述】:

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

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

【问题讨论】:

    标签: c# propertygrid uitypeeditor


    【解决方案1】:

    一种解决方案是声明一个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
    {
    }
    

    【讨论】:

    • 感谢您的回复。这可能是我需要的。我会检查一下,如果符合我的需要,我会将其标记为正确答案。
    • 这对我不起作用 :-( 我什至定义了 CanConvertFrom 方法,如果源类型是字符串则返回 false,但不调用此方法。
    • @jstuardo - 你的上下文可能不同,问另一个问题。
    • 不..上下文是一样的...但没关系...我将停止使用 PropertyGrid...这是一个真正的痛苦,不仅是这个问题,还有我遇到的其他问题曾自定义 PropertyGrid 行为。
    【解决方案2】:

    我找到了这样的解决方法(PropertyGrid 的PropertyValueChanged 事件):

    private void propertyGridNewBonus_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
            {
                switch (e.ChangedItem.Label)
                {
                    case "somePropertyLabel":
                        newBonus.somePropertyValue = e.OldValue.ToString();
                        break;
                    default:
                        break;
                }
            }
    

    只是在用户在 propertyGrid 中编辑旧值时恢复旧值。 这看起来像值是可编辑的,但在提交旧值后会恢复,因此更改它的唯一方法是使用自定义 TypeConverter、UITypeEditor 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多