【问题标题】:Visual Studio User Contol DesignVisual Studio 用户控件设计
【发布时间】:2011-08-24 15:29:07
【问题描述】:

如果您有一个用户控件,其中包含 public int number = 10; 之类的字段,当您在 VS 2010 和 C# 中使用设计器时,是否可以在属性框中显示该值?

【问题讨论】:

    标签: c# .net user-controls windows-forms-designer visual-studio-designer


    【解决方案1】:

    你必须把你的变量变成一个属性。尝试添加 Get Set,有时您必须添加正确的属性(引用 System.ComponentModel 类)

    private int _Number = 10;
    
    [DefaultValue(10)]
    [Description("This is a number.")]
    public int Number
    {
      get { return _Number; }
      set { _Number = value; }
    }
    

    注意:DefaultValue 属性用于设置设计器是否为粗体。它实际上并没有设置默认值。

    【讨论】:

    • 如果您想从属性检查器中隐藏公共属性,您只需要使用 (false) 应用 Browsable 属性。
    • 隐藏像 BorderStyle 这样的默认属性怎么样?
    • @Mark 不同的问题。您必须自己覆盖该属性并添加 Browsable(false) 属性。
    • 好吧听起来很简单会接受这个答案,因为它是第一个
    【解决方案2】:

    使用 System.ComponentModel;

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

    如果你想要某个类别下的属性

    [Category("My Properties")] 
    public string MyCustomProperty{ get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多