Scope of controls inside Silverlight XAML
I have defined a textbox with x:Name="txtMyTextBox" inside UserControl called MyView. I've noticed that I can do the following:开发者_StackOverflow社区
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?
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.
精彩评论