我有一个属性网格,其中一个属性使用UITypeEditor来编辑值(在表单上)。
但是,该属性仍然是可编辑的,我不希望如此。有办法这样做吗?我看了这个类似的问题Propertygrid UIEditor通过键盘禁用值编辑,但它不能解决我的问题,因为解决方案是一个简单的下拉列表使用TypeConverter。
发布于 2015-05-12 15:08:30
一种解决方案是声明一个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
{
}发布于 2016-03-09 09:23:00
我找到了这样的解决办法(PropertyValueChanged event for PropertyGrid):
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等。
发布于 2022-05-16 19:59:57
为了将来的使用,因为这些答案不再是它的方法了。如果不应用ReadOnly,则将其设置为true或default为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();
}
}https://stackoverflow.com/questions/30194161
复制相似问题