且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在propertyGrid中隐藏属性

更新时间:2023-02-15 09:33:27

这违反了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