Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot see named Silverlight control in code

In my first few hours with Silverlight 3, as an avid WPF user, I am greatly disappointed at the many things it doesn't support. This seems like an odd issue to me and it's so generic that I cannot find anything online about it.

I have the following XAML:

<controls:TabControl x:Name="workspacesTabControl" Grid.Row="1"
Background="AntiqueWhite" ItemsSource="{Binding Workspaces, ElementName=_root}"/>

However, I cannot see the workspacesTabControl in code-behind. I thought maybe IntelliSense is just being mean and tried to go ahead and compile it anyway, but got an error:

Error   1   The name 'workspacesTabControl' does not exist in the current context

How do I access controls in code-behind?

EDIT: I realized I've pasted the wrong error - I have two controls inside the UserControl called workspacesTabControl and menuStrip. I cannot get to either one of them by their name in the code-behind.

Just in case, here is the XAML for the menuStrip:

<controls:TreeView Grid.ColumnSpan="2" Height="100" x:Name="menuStrip"
                   ItemContainerStyle="{StaticResource MenuStripStyle}"
                   ItemsSource="{Binding Menu, ElementName=_root}"/>

EDIT AGAIN:

I'm not sure if this is helpful, but I've taken a look at the InitializeComponent() code and here's what I saw:

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Windows.Application.LoadComponent(this, new System.Uri("/SapphireApplication;component/SapphireMain.xaml", System.UriKind.Relative));
}

It seems that it simply loads the XAML when it runs (not before or during compilation) so the menuStrip and workspacesTabControl names don't actually get registered anywhere (as they usually are in WPF/win Forms). Could that attribute be a problem? And where do I get rid of this requirement for all the future UserControls I make?

like image 534
Alexandra Avatar asked Nov 29 '22 04:11

Alexandra


2 Answers

Check the properties in VS for the xaml file itself... make sure the Build Action is set to Page.

like image 69
grf Avatar answered Dec 05 '22 17:12

grf


As ridiculous as it may sound, I have resorted to using FindName() method to access named items in code-behind:

this.FindName("workspacesTabControl") as TabControl

I realize that this is a ridiculous way but I am forced to use this for now. Please let me know if someone else has encountered this problem and have come up with a better solution!

like image 24
Alexandra Avatar answered Dec 05 '22 18:12

Alexandra