Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my controls inside a UserControl private?

I have a user control with a ComboBox and a TextBox. Everything is working great except I noticed that from my user control's instance object, I can access those two controls. They shouldn't be accessible except via my own exposed properties.

like image 717
ScottG Avatar asked Nov 18 '08 21:11

ScottG


People also ask

What is user control?

User controls. User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

What is user control in c sharp?

Definition of C# User Control. C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project.

What is a user control windows forms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


1 Answers

You can use the x:FieldModifier attribute on your controls, thusly:

<TextBox x:Name="textBox1" x:FieldModifier="private" /> 

Interestingly the MSDN page for x:FieldModifier doesn't give "private" as a valid value for the attribute, but I've tested it and it works.

like image 89
Matt Hamilton Avatar answered Nov 03 '22 07:11

Matt Hamilton