如何使字段在运行时可编辑
是否可以在运行时应用 EditableAttribute?我想让某些属性仅在用户具有某种角色时才可编辑。不幸的是 EditatbleAttribute 是密封的。我可以尝试通过反射在运行时应用它,但也许有更合适的方法来做到这一点。感谢您的任何建议。
此致
Is it possible to apply EditableAttribute at runtime? I want to make some properties editable only when the user has some role. Unfortunately EdiatbleAttribute is sealed. I can try to apply it at runtime by reflection but maybe there's more proper way to do this. Thanks for any advices.
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个黑客可用:如何在运行时设置属性值 - 以及如何解决愚蠢的错误
我能够通过此代码更改属性的
ReadOnly
属性值。语句propertyGrid1.SelectedObject = new Student();
可以替换为propertyGrid1.SelectedObject = myStudent
即您可以修改现有对象的属性。另外,看看类似的问题:在运行时更改属性的参数
There is a hack available at: How to Set Attribute Value at Runtime-- and How to Work around a Silly Bug
I was able to this code to change the
ReadOnly
attribute value of the property. The statementpropertyGrid1.SelectedObject = new Student();
can be replaced bypropertyGrid1.SelectedObject = myStudent
i.e. you can modify the properties of an existing object.Also, have a look at a similar question: Change Attribute's parameter at runtime
我认为一个不错的选择是为您需要使用的控件(TextBox 等)创建一个扩展助手,如果 IsReadOnly 属性为 true(Editable(false)),则生成一个禁用的文本框。
I think a good option is to create an extension helper for the control you need to use (TextBox etc) and if the IsReadOnly property is true (Editable(false)) then generate a disabled textbox.