简体   繁体   English

"从 VS 设计器中隐藏 WinForm UserControl 自定义属性"

[英]Hide WinForm UserControl custom property from VS designer

Visual Studio is incorrectly calling my UserControl's custom properties at design time. Visual Studio 在设计时错误地调用了我的 UserControl 的自定义属性。

I have read many of the posting about using the [Browsable( false )]<\/strong> and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]<\/strong> attributes, but this has not worked for me.我已经阅读了许多关于使用[Browsable( false )]<\/strong>和[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]<\/strong>属性的帖子,但这对我不起作用。

To reproduce this problem, using Visual Studio, create a new Windows Forms Application<\/strong> , then add a User Control<\/strong> to your project, and drag that User Control onto your Form.要重现此问题,请使用 Visual Studio 创建一个新的Windows Forms Application<\/strong> ,然后将一个用户控件<\/strong>添加到您的项目中,并将该用户控件拖到您的窗体上。 Add a public custom property<\/strong> to your User Control, as shown below.公共自定义属性<\/strong>添加到您的用户控件,如下所示。

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    [Browsable( false )]
    [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
    public bool AreYouThere
    {
        get
        {
            MessageBox.Show( "Yes I Am Here!" );
            return true;
        }
    }
}

In order to hide a property from every place possible you have to mark it with those attributes 为了从可能的每个地方隐藏属性,您必须使用这些属性标记它

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public class CustomDesigner : ControlDesigner
{
    private static string[] RemovedProperties = new[]
    {
        "AccessibilityObject","AccessibleDefaultActionDescription","AccessibleDescription",
        "AccessibleName","AccessibleRole","AllowDrop","Anchor","AutoEllipsis","AutoScrollOffset",
        "AutoSize","AutoSizeMode","FlatAppearance", "FlatStyle",
        "TextAlign","TextImageRelation","UseCompatibleTextRendering",
        "UseMnemonic","UseWaitCursor"
    };

    public CustomDesigner() { }

    protected override void PreFilterProperties(IDictionary properties)
    {
        foreach (string prop in RemovedProperties)
        {
            properties.Remove(prop);
        }
        base.PreFilterProperties(properties);
    }
}

[ToolboxItem(true)]
[DesignerCategory("code")]
[Designer(typeof(CustomDesigner))]
public partial class NewButton : Button
{
    public Color OnHoverBackColor
    {
        get { return _onHoverBackColor; }
        set
        {
            _onHoverBackColor = value;
            Invalidate();
        }
    }
}

Do not set the default value for the property as you want it.不要根据需要设置属性的默认值。 In your example, set the property AreYouThere<\/strong> to false\/true and in the parent or whereever you are using it you instanceOfUserControl1.AreYouThere = true\/false<\/code> in say Load event.在您的示例中,将属性AreYouThere<\/strong>设置为 false\/true,并且在父级或您使用它的任何地方,您instanceOfUserControl1.AreYouThere = true\/false<\/code>在说 Load 事件中。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM