views:

13

answers:

1

I have defined a textbox with x:Name="txtMyTextBox" inside UserControl called MyView. I've noticed that I can do the following:

MyView myView = new MyView();
myView.txtMyTextBox.Text = "something";

Why txtMyTextBox is accessible that way? Is it public or internal field? Can I make it private?

+1  A: 

The Silverlight XAML designer creates fields for named elements so that you can access them from the code behind. You can see the generated file if you go into the code behind and choose InitializeComponent from the method selection drop down at the top. It's kept in a partial file. In the past, designer generated fields have been scoped as private, but for some reason I cannot fathom, the current crop of XAML designers (VS2010, Blend) creates it as internal.

You can change the visibility of the field that gets generated by using the x:FieldModifier attribute but you probably don't need to worry about it. If you need to, you should expose a public property from your user control that wraps access to it instead.

Josh Einstein