Skip to main content
 首页 » 编程设计

c#之在设计时像 ReadOnly 一样更改属性属性 (PropertyGrid)

2024年02月24日16mayingbao

我开发了一个自定义控件,其中包含如下所示的一些字段:

ControlKind, Field1, Field2

我想更改每个 controlKind 字段之一的属性,
比如:如果 controlKind == useField1,则 Field1 显示,Field2 隐藏在 properyGrid(或只读或过滤器)中

我可以吗?

提前致谢

请您参考如下方法:

实现 ICustomTypeDescriptor,或注册 TypeDescriptionProvider 并从中返回自定义类型描述符,或应用 TypeConverterAttribute。

在所有这三种情况下,您都需要实现/覆盖 GetProperties 方法以返回实际属性的筛选子集。 (在 TypeConverter 方法中,您还必须覆盖 GetPropertiesSupported 以返回 true。)