【问题标题】:Add Properties to a base class so they show on Visual Studio's Properties window?将属性添加到基类,以便它们显示在 Visual Studio 属性窗口中?
【发布时间】:2011-11-01 01:55:27
【问题描述】:

我有这门课:

public class CameraWindow : PictureBox

如何添加将显示在此处的属性:

这很容易,但我找不到这样做的方法=\

【问题讨论】:

    标签: c# .net visual-studio-2010 properties base-class


    【解决方案1】:

    您需要使用Browsable 属性注释属性。

    例如

    public class CameraWindow : PictureBox
    {
        [Browsable(true)]
        public int MyProperty{get;set;}
    }
    

    如果您希望您的属性出现在属性窗口的特定分组中,您还可以添加Category 属性。

    【讨论】:

    • 'System.ComponentModel.BrowsableAttribute' does not contain a constructor that takes 0 arguments
    • @Danpe:查看我修改后的答案。
    【解决方案2】:

    使用 System.ComponentModel;

    [Browsable(true)]
    public bool SampleProperty { get; set; }
    

    【讨论】:

    • 谢谢,它有效,但它显示在Misc 类别中。我可以更改类别吗?
    • @Danpe:是的 - 请参阅我回答的最后一部分。
    • 是的,简单使用 [Category("My Properties")] public string MyCustomProperty{ get;放; }
    【解决方案3】:

    您可以使用BrowsableAttribute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多