且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问在C#中的usercontrol属性

更新时间:2022-12-03 12:29:23

干净的方法是为你的用户控件的属性暴露所需的性能,例如:

Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:

class MyUserControl
{
  // expose the Text of the richtext control (read-only)
  public string TextOfRichTextBox
  {
    get { return richTextBox.Text; }
  }
  // expose the Checked Property of a checkbox (read/write)
  public bool CheckBoxProperty
  {
    get { return checkBox.Checked; }
    set { checkBox.Checked = value; }
  }


  //...
}

在这种方式,您可以控制​​要揭露和是否应读/写或只读的属性。 (当然你应该为属性,使用更好的名称取决于它们的含义)。

In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).

这种方法的另一个优点是,它隐藏内部实现用户控制的。你曾经想用一个不同的交换你的富文本控制,你会不会打破你的控制呼叫者/用户。

Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.