开发者

C# PropertyGrid - How to make array elements read only?

开发者 https://www.devze.com 2023-04-07 06:20 出处:网络
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.

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号