PropertyGrid [英] PropertyGrid

查看:108
本文介绍了PropertyGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在类中有一个字段,用户可以通过PropertyGrid进行修改.

因此,我设置了一个公共属性,可以通过PropertyGrid访问该属性,用户可以分配一个值.

问题是我想确保用户通过PropertyGrid分配的值应该是只读的,即不应通过对象引用来修改属性.

换句话说,我想以某种方式确保用户只能通过PropertyGrid分配属性的值,并且使用此对象的任何人都不能直接对其进行修改.

该属性应该是只读的,但是从PorpertyGrid可以修改它.有什么办法可以做到这一点?可以通过PropertyGrid修改私有属性吗?

Hi,

I have a field in a class which the user can modify via a PropertyGrid.

So I made a public property which can be accessed via the PropertyGrid and the user can assign a value.

The problem is that I want to make sure that the value assigned by the user via the PropertyGrid should be readonly i.e. the property should not be modified through the object reference.

In other words I somehow want to make sure that user only can assign the value of the property via the PropertyGrid and anyone who is using this object should not be able to modify it directly.

This property should be readonly but from the PorpertyGrid one should be able to modify it. Is there any way to achieve this? Is there any way to modify private properties via the PropertyGrid?

推荐答案

不幸的是,没有.如果属性网格可以访问它,那么任何其他代码也可以访问.

但是您可以使用一个技巧,该技巧特定于属性网格.删除公共属性,并使用ICustomTypeDescriptorTypeDescriptionProvider将其替换为动态运行时属性.我在这里有一篇文章介绍了如何执行此操作:

http://www.codeproject.com/KB/cs/UsingTypeDescriptionProv.aspx [ ^ ]

这不是一个很简单的任务,但也不太复杂.
Unfortunately no. If the property grid can access it, then any other code can too.

But there is a trick you can use that''s specific to the property grid. Remove the public property and replace it with a dynamic runtime property using ICustomTypeDescriptor and TypeDescriptionProvider. I''ve got an article here that explains how to do this:

http://www.codeproject.com/KB/cs/UsingTypeDescriptionProv.aspx[^]

It is not a very simple task but it''s not too complex either.


这篇关于PropertyGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆