Reputation: 23
I have a property grid and one of the properties uses a UITypeEditor to edit the value (on a form).
However the property is still editable, which I do not want. Is there a way to do this? I looked at this similar question Propertygrid UIEditor disabling value editing through Keyboard but it does not solve my problem as the solution is a simple dropdown list using TypeConverter.
Upvotes: 2
Views: 3156
Reputation:
For future use as these answer are not how it's done anymore. Set ReadOnly to true or default is false when not applied.
[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();
}
}
Upvotes: 1
Reputation: 1753
I found a workaround like this (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;
}
}
just restoring old value while user edits it in propertyGrid. This looks like value is editable, but after comitting old value is restored so the only way to change it is to use custom TypeConverter, UITypeEditor or so.
Upvotes: 0
Reputation: 138925
One solution is to declare a TypeConverter that does ... nothing, something like this:
This is the class you want to edit:
public class MyClass
{
[Editor(typeof(MyClassEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(MyConverter))]
public string MyProperty { get; set; }
}
This is the custom 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 is the famous converter that took me days to write:
// this class does nothing on purpose
public class MyConverter : TypeConverter
{
}
Upvotes: 5