开发者

Should a User Control's Controls be backed by Properties?

Hopefully I am stating that right. I have a WinForm(3.5) app that has 1 Form that is broke into 开发者_运维百科two regions. 1 is the Navigation and the other, a Panel, is the Content. You select what you want in the Navigation Portion, i.e. Demographics, and then it embeds a UserControl containing all the Demographics controls in the Panel.

What I am asking is if each User Control should have a Property for each Control on it. Example: ucDemographics has a textbox, named txtCity. Should there be a Property to store the value of txtCity and allow my Form and other User Controls to access it?

Is that the generally accepted 'Best Practice'?

If not, what is?


It depends on what you want to achieve with your UserControl.

Normally you wouldn't expose the txtCity because the caller could manipulate everything about the textbox then. In most scenarios, you would only expose the current text á la

public string CityText
{
    get { return this.txtCity.Text; }
}


No, that's not really a best practice. The intention of a user control is to compose a new control with its own behavior. You should at most have "several" properties, methods and events that are public and allows a form to interact with the new control. If you find that the only good way to work with it is by exposing its constituent controls that you're better off not using a UserControl but just place the controls on the form directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜