Scope of controls within Silverlight XAML
I have defined a textbox with x: Name = "txtMyTextBox" inside a UserControl named MyView. I noticed that I can do the following:
MyView myView = new MyView();
myView.txtMyTextBox.Text = "something";
Why is txtMyTextBox available this way? Is this a public or internal field? Can I make it private?
a source to share
The Silverlight XAML designer creates fields for named elements so that you can access them from code. You can see the generated file if you go to the code behind and select InitializeComponent
from the method selection dropdown at the top. It is stored in a partial file. Previously, the fields generated by the designer were limited as private, but for some reason I can't figure out that now its creators XAML designers (VS2010, Blend) are created as internal .
You can change the visibility of the field that is generated using the attribute x:FieldModifier
, but you probably don't need to worry about that. If you need to, you must expose the public property from your custom control, which exchanges its access instead.
a source to share