Question

I have two fields that are of the same type in my property-grid. However, one is read-only, the other is editable.

Both of these fields are of a custom type, and thus have a custom UITypeEditor, which puts the elipsis ([...]) button on the field.

[
     CategoryAttribute("5 - Wind"),
     DisplayName("Factored Area"),
     Description("The factored area for the segment."),
     EditorAttribute(typeof(umConversionTypeEditor), typeof(UITypeEditor)),
     TypeConverter(typeof(umConversionTypeConverter)),
     ReadOnly(true)
]
public FactoredAreaClass FactoredArea { ... }

[
     CategoryAttribute("5 - Wind"),
     DisplayName("Factored Area Modifier"),
     Description("The factored area modifier."),
     EditorAttribute(typeof(umConversionTypeEditor), typeof(UITypeEditor)),
     TypeConverter(typeof(umConversionTypeConverter))
]
public FactoredAreaClass FactoredAreaMod { ... }

In this example, FactoredAreaMod is available to be edited, but BOTH have the elipsis, which will cause great confusion with the users. Any way to turn that off??

Was it helpful?

Solution

Use the ReadOnly attribute. This marks it as design-time read-only while keeping it read/write for runtime use.

Also, you should either apply the Editor attribute to the type rather than the properties. There's no gain in applying it to a property if you don't want that property to be editable.

OTHER TIPS

Thanks to Jeff Yates, I came up with an alternate solution. Here's how I solved it...

The biggest issue was that the EditorAttribute was actually assigned in the FactoredAreaClass. I put it in the raw example just to show that there was an editor attribute assigned.

[
    CategoryAttribute("5 - Wind"),
    DisplayName("Factored Area"),
    Description("The factored area for the segment."),
    EditorAttribute(typeof(UITypeEditor), typeof(UITypeEditor)), // RESET THE UITYPEEDITOR to "nothing"
    ReadOnly(true)
]
public FactoredAreaClass FactoredArea { ... }

[
    CategoryAttribute("5 - Wind"),
    DisplayName("Factored Area Modifier"),
    Description("The factored area modifier."),
    // the EditorAttribute and TypeConverter are part of FactoredAreaClass
]
public FactoredAreaClass FactoredAreaMod { ... }

The trick is not to use the Modal style when the bounded property is readonly. Luckily for us, the context is provided in the GetEditStyle method. A simple code will do the job:

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
  return context.PropertyDescriptor.IsReadOnly 
          ? UITypeEditorEditStyle.None 
          : UITypeEditorEditStyle.Modal;       
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top