3

I have a PropertyGrid to which I add a array of bool-values. The array itself is marked as ReadOnly, which is recognized properly by the property grid.

BUT: If I expand the array in the Grid, all the items are editable by the user. Of course that's not what I want. If the array itself is marked a s ReadOnly all its elements shall be as well!

Is there any way to achieve this behavior in the PropertyGrid?

Boris
  • 8,551
  • 25
  • 67
  • 120
  • If your binding the collection to the propertygrid, then you can have that collection indexers set property private. That way none can set. – Zenwalker Sep 26 '11 at 08:47
  • 1
    What do you mean, your array is read-only? Are you wrapping it using ReadOnlyCollection? – Luke Girvin Sep 26 '11 at 08:51

2 Answers2

3

You can define your own TypeConverter. Using a TypeConverter, you can control the properties that the PropertyGrid shows, and their behavior.

GvS
  • 52,015
  • 16
  • 101
  • 139
2

The readonly keyword doesn't work the way you think it does:

using System;

class Program {
    static readonly bool[] arr = { false, true };

    static void Main(string[] args) {
        arr[0] = true;
    }
}

Yes, use TypeConverter to alter the behavior of types in PropertyGrid. Or just give it the [Browsable(false)] attribute because nobody wants to look at an array of booleans anyway.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536