PropertyGrid UITypeEditor Disable cell edit
Asked Answered
M

3

2

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.

Miguelinamiguelita answered 12/5, 2015 at 14:40 Comment(0)
S
1

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();
    }
}
Serin answered 16/5, 2022 at 19:59 Comment(0)
V
5

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
{
}
Vaca answered 12/5, 2015 at 15:8 Comment(4)
Thanks for your reply. This may be what I need. I'll check this out and if does what I need, I'll mark this as the correct answer.Miguelinamiguelita
This did not work for me :-( And I have even defined CanConvertFrom method, returning false if source type is string, but this method is not called.Anhedral
@Anhedral - your context is probably different, ask another question.Vaca
No.. context is the same... but never mind... I will stop using PropertyGrid... this is a real pain, not only with this problem but also with other problems I have had in customizing PropertyGrid behaviour.Anhedral
S
1

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();
    }
}
Serin answered 16/5, 2022 at 19:59 Comment(0)
N
0

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.

Numerable answered 9/3, 2016 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.