如何在propertyGrid中隐藏属性 [英] how to hide properties in propertyGrid

查看:125
本文介绍了如何在propertyGrid中隐藏属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"A"的类,并且该类继承自Windows.Forms.PictureBox

"A"具有公共属性

如果我们按以下方式命名A的属性,则: br> A1

I have a Class named "A" and this class inherits from Windows.Forms.PictureBox

"A" has public properties

if we name properties of A as below:
A1

A2
A3

我们可以设置A1,A2,A3和A3的值.运行时并将其命名为MyA

,然后我有一个PropertyGrid名称Pr1

A2
A3

we can set values of A1, A2, A3 and All inherited properties from pictureBox

I create an instance of A in runtime and name it MyA

and then I have a PropertyGrid name Pr1

Pr1.selectedObject = MyA 


Pr1显示了所有MyA的属性,包括所有来自PictureBox的Inheritec,我希望Pr1仅显示A1,A2和A3并隐藏其余部分.我该怎么做????


Pr1 is showing all properties of MyA including all inheritec from PictureBox

I want Pr1 to show only A1,A2 and A3 and hide the rest. how can I do that????

推荐答案

这违反了OOP原则.基础对象存储区的所有属性等都存在于派生对象中.

但是,Microsoft始终会自己这样做! PictureBox是一个很好的例子.它在属性网格中或通过intellisense在代码编辑器中没有可浏览的Text属性,但是由于它是从具有Text属性的System.Windows.Forms.Control派生而来的,因此仍然存在.若要实现此目的,请对不希望看到的属性,事件和方法执行类似的操作.
This is against OOP principals. All properties etc of the base object shold be present in the deriving object.

However, Microsoft do this themselves all the time! The PictureBox is a good example. It has no browsable Text property in the property grid or in the code editor via intellisense, but it''s still there as it derives from System.Windows.Forms.Control which has the Text property.

The way to achieve this is to do something like this with properties, events and methods that you don''t want to be visible.
using System;<br />using System.ComponentModel;<br />using System.Drawing;<br />using System.Windows.Forms;<br /><br />public class MyPictureBox : PictureBox<br />{<br />    [Browsable(false)]<br />    [EditorBrowsable(EditorBrowsableState.Never)]<br />    [Obsolete("This property is obselete", true)]<br />    public new Color BackColor<br />    {<br />        get { return base.BackColor; }<br />        set { base.BackColor = value; }<br />    }<br />}

这将使BackColor属性消失" IF MyPictureBox为在单独的装配体中.如果在同一程序集中,或者作为对另一个项目的项目引用添加,则它将不起作用-它必须是对单独dll的引用.

This will make the BackColor property ''disappear'' IF MyPictureBox is in a seperate assembly. It will not work if in the same assembly, or added as a project reference to another project - it must be a reference to a seperate dll.


解决此问题的最佳方法是使用只显示您要显示的属性的包装器类
Probably the best way to approach this is to use a wrapper class that only displays the properties you want to show


这篇关于如何在propertyGrid中隐藏属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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