只读属性网格
我在编写的应用程序中使用 PropertyGrid,以允许用户查看并且有时编辑对象的实例。有时,用户可能以读/写模式打开文件,他们可以通过属性网格对文件进行更改。在其他情况下,他们可能以只读模式打开文件,并且不应该能够通过 PropetyGrid 对对象进行任何更改。我的类还具有通过实现 ICustomTypeDescriptor 返回的动态属性。这就是为什么我真的想利用 PropertyGrid 控件的内置灵活性。
似乎没有一种简单的方法可以将属性网格设置为只读模式。如果我禁用 PropertyGrid,这也会阻止用户滚动列表。所以我认为最好的方法是在运行时将 ReadOnlyAttributes 添加到属性中。还有其他办法吗?
I'm using a PropertyGrid in an application I am writing to allow users to view and sometimes edit instances of my objects. Sometimes the user may have a file open in read/write mode where they can make changes to the file through the property grid. In other cases they may have a file open in read only mode, and should not be able to make any changes to the objects through the PropetyGrid. My classes also have dynamic properties which are returned by implementing ICustomTypeDescriptor. Which is why I really want to take advantage of the built in flexibility of a PropertyGrid control.
There doesn't seem to be an easy way to set a Property-grid to a read only mode. If I disable a PropertyGrid this also prevents the user from scrolling the list. So I'm thinking the best way to do this is to add ReadOnlyAttributes to the properties at run-time. Is there some other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些不关心属性网格变灰的人,我找到了一个非常快速的解决方案。
I have found a very quick solution for thoses who do not care about the propertygrid being grayed out.
由于您正在实现
ICustomTypeDescriptor
,因此无需添加任何属性;您只需覆盖PropertyDescriptor
上的IsReadOnly
即可。我认为编写一个模仿(通过 ICustomTypeDescriptor 和 TypeConverter)包装类型但始终返回只读 PropertyDescriptor 的中间类型应该非常简单> 实例?如果您想要一个示例,请告诉我(尽管它不是微不足道的)。您可能还想检查类似的内容是否提供了构建它的功能。
Since you are implementing
ICustomTypeDescriptor
there is no need to add any attributes; you can just overrideIsReadOnly
on thePropertyDescriptor
. I'm thinking it should be pretty simple to write an intermediary type that mimics (viaICustomTypeDescriptor
andTypeConverter
) a wrapped type but always returns readonlyPropertyDesciptor
instances? Let me know if you want an example (it isn't trivial though).You might also want to check whether something like this offers it built it.
我最终从 PropertyGrid 继承并在选择属性时选择父类别。
简单,不需要使用TypeDescriptor。
I ended up inheriting from PropertyGrid and select the parent category whenever a property is selected.
Simple and no need to use TypeDescriptor.
我的建议是编写一个继承自 propertygrid 控件的自定义控件,并在该自定义控件中具有只读布尔值,然后覆盖一些内容并检查,if(readonly) 然后取消操作
My advice would be to write a custom control that inherits from the propertygrid control, and in that custom control, have a boolean value of readonly, and then override some things and check, if(readonly) then cancel the action
我遇到了这个。我想要一个只读但不灰显的控件。
从属性网格控件继承并通过添加以下代码来覆盖按键来创建您自己的只读版本
I ran into this one. I wanted a control that was read only but not grayed out.
Inherit from the property grid control and create your own read only version by adding the following code to override key presses