C# 在自定义控件中隐藏不需要的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:27:43  来源:igfitidea点击:

Hiding unwanted properties in custom controls

c#.netwinformsgdi+

提问by Joan Venge

Is this the way to hide properties in derived controls?

这是在派生控件中隐藏属性的方法吗?

public class NewButton : Button

public class NewButton : Button

...

...

[Browsable ( false )]
public new ContentAlignment TextAlign { get; set; }

Also this hides the property in the Properties window in the designer but how can I also hide the property in code?

此外,这会在设计器的“属性”窗口中隐藏该属性,但如何在代码中隐藏该属性?

采纳答案by Marc Gravell

From code, the closest you can do it to hide it, and perhaps make it a pain to call directly - note that even when hidden it is callable, and none of this will work past a cast:

从代码中,您可以最接近地隐藏它,并且可能使直接调用变得很痛苦 - 请注意,即使隐藏它也是可调用的,并且这些都不会通过演员表起作用:

// about the closest you can do, but not really an answer
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("just cast me to avoid all this hiding...", true)]
public new ContentAlignment TextAlign { get; set; }

Personally, I wouldn't bother. It isn't robust (just cast).

就我个人而言,我不会打扰。它不健壮(只是铸造)。

回答by Andrew Kennan

Maybe what you want to do is derive from ContainerControl or UserControl, add a Button to that control and just expose those parts of the Button interface you want to keep.

也许您想要做的是从 ContainerControl 或 UserControl 派生,向该控件添加一个 Button 并只公开您想要保留的 Button 界面的那些部分。

回答by Henk Holterman

No, you can remove them from the designer (as shown) but you cannot really hide them form code as that would violate the substitution principle. It has been asked & answered many times here, see for example this SO question.

不,您可以从设计器中删除它们(如图所示),但您不能真正将它们隐藏在代码中,因为这会违反替换原则。这里已经被多次询问和回答,例如参见这个 SO question

回答by Drew Noakes

You can use the [EditorBrowsable]attribute, as documented here.

您可以使用该[EditorBrowsable]属性,如此处所述

[EditorBrowsable(EditorBrowsableState.Never)]
public bool HideMeInIntellisense
{
    // ...

From the documentation:

从文档:

...the IntelliSense engine in Visual Studio uses this attribute to determine whether to show a property or method.

...Visual Studio 中的 IntelliSense 引擎使用此属性来确定是否显示属性或方法。

However, users can override this in VS settings. ReSharper also has a setting that controls whether this attribute is honoured in its IntelliSense.

但是,用户可以在 VS 设置中覆盖它。ReSharper 还有一个设置,用于控制是否在其 IntelliSense 中遵循此属性。

Out of curiousity, why do you want to hide something from users? Just because a member is hidden in the way described above doesn't mean you couldn't use it in code and compile it successfully. It just inhibits the discoverability of the member.

出于好奇,你为什么要对用户隐藏一些东西?仅仅因为成员以上述方式隐藏并不意味着您不能在代码中使用它并成功编译它。它只是抑制了成员的可发现性。

回答by CLS

Why don't you make it private? It guarantees that ancestors will not see it. Edit: In this case you have to inherit a new class from the base and use your new class, which now hides ths property.

你为什么不把它设为私有?它保证祖先不会看到它。编辑:在这种情况下,您必须从基类继承一个新类并使用您的新类,它现在隐藏了 ths 属性。

public class MyTextBox: TextBox
{
...
        private new ContentAlignment TextAlign
        {
          get { return base.ContentAlignment; }
          set { base.ContentAlignment = value; }
        }
}