PropertyGrid UITypeEditor 禁用单元格编辑
PropertyGrid UITypeEditor Disable cell edit
我有一个 属性 网格,其中一个属性使用 UITypeEditor 来编辑值(在表单上)。
但是 属性 仍然可以编辑,这是我不想要的。有没有办法做到这一点?我查看了这个类似的问题 Propertygrid UIEditor disabling value editing through Keyboard 但它没有解决我的问题,因为解决方案是使用 TypeConverter 的简单下拉列表。
一个解决方案是声明一个 TypeConverter 什么都不做,像这样:
这是您要编辑的 class:
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
{
}
我找到了这样的解决方法(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 等。
供将来使用,因为这些答案已不再是如何完成的。将 ReadOnly 设置为 true 或在未应用时默认为 false。
[Browsable(true)]
[ReadOnly(true)]
[Description("Behind the scenes identifier for a record.")]
[Category("Record Info")]
[DisplayName("Identifier")]
public string Identifier
{
get
{
return _identifier;
}
set
{
_identifier = value.Trim();
}
}
我有一个 属性 网格,其中一个属性使用 UITypeEditor 来编辑值(在表单上)。
但是 属性 仍然可以编辑,这是我不想要的。有没有办法做到这一点?我查看了这个类似的问题 Propertygrid UIEditor disabling value editing through Keyboard 但它没有解决我的问题,因为解决方案是使用 TypeConverter 的简单下拉列表。
一个解决方案是声明一个 TypeConverter 什么都不做,像这样:
这是您要编辑的 class:
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
{
}
我找到了这样的解决方法(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 等。
供将来使用,因为这些答案已不再是如何完成的。将 ReadOnly 设置为 true 或在未应用时默认为 false。
[Browsable(true)]
[ReadOnly(true)]
[Description("Behind the scenes identifier for a record.")]
[Category("Record Info")]
[DisplayName("Identifier")]
public string Identifier
{
get
{
return _identifier;
}
set
{
_identifier = value.Trim();
}
}