代码之家  ›  专栏  ›  技术社区  ›  jay_t55

用户控件-自定义属性

  •  62
  • jay_t55  · 技术社区  · 16 年前

    我需要我的用户控件的用户能够更改某些字符串值,并且我希望他们能够将用户控件添加到表单中,然后单击它以打开属性窗格,在其中显示我的用户控件的自定义属性。

    如何为用户控件拥有自己的自定义属性?例如:

    3 回复  |  直到 11 年前
        1
  •  112
  •   AustinWBryan ravenspoint    5 年前

    您可以通过属性上的属性执行此操作,如下所示:

    [Description("Test text displayed in the textbox"),Category("Data")] 
    public string Text {
      get => myInnerTextBox.Text;
      set => myInnerTextBox.Text = value;
    }
    

    Here's a more complete MSDN reference ,包括类别列表。

        2
  •  49
  •   Hans Passant    5 年前

    非常简单,只需添加一个属性:

    public string Value {
      get { return textBox1.Text; }
      set { textBox1.Text = value; }
    }
    

    使用Text属性有点棘手,UserControl类 intentionally hides

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Bindable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text {
      get { return textBox1.Text; }
      set { textBox1.Text = value; }
    }
    
        3
  •  7
  •   Community Mohan Dere    10 年前

    只需将公共属性添加到用户控件。

    你可以加上 [Category("MyCategory")] [Description("A property that controls the wossname")] 属性,它应该显示在属性面板中。

    推荐文章