views:

549

answers:

7

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?

A: 

You should be able to see it in the codebehind, that part works the same as WPF, maybe if you fix the problem with the menuStrip, then visual studio will be able to build the xaml paty of the page and ull be able to access the tabcontrol

Neil
A: 

I've seen the same problem in my Silverlight development. Specific to my problem my named controls were nested inside other controls (i.e. a datagrid) and I was unable to access them in my code behind. Any named controls at the same nesting level or above the previously mentioned datagrid worked fine but anything inside it was lost into the abyss.

Ragepotato
A: 

When you first create a control, Visual Studio does not pick it up with intellisense. However, after you try to build the project, it should become availble. You can also just type the name in without intellisense and then build it. Haven't verified this, but I heard this was on the list of things to fix in SL4.

That being said, if you name a control inside of a datatemplate, that control is not directly accessible in code-behind. This is the same for WPF, though.

Jacob Adams
A: 

As already mentioned, it should just appear in Intellisense, however the fact that you're getting an error related to something else, i.e. "menuStrip" is probably interfering with Intellisense. Resolve that error and you'l probably find that you can access the "workspacesTabControl" control.

Are you possibly using some sample code or something where they've named a control "menuStrip" and you've renamed it?

Good luck

Ola Karlsson
A: 

Check that you don't have any controls using the same class name as a namespace name. For example:

namespace Solution.ProjectName.workspacesTabControl
{  
    public class workspacesTabControl
    {
        ...
    }
}

This will also give you this error.

Good luck,
Mark

Mark Cooper
This is not the case. My definitions are like so: namespace SapphireApplication { public partial class SapphireMain : UserControl {
Alexandra
+1  A: 

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!

Alexandra
+3  A: 

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

grf
I was having this problem, and this worked for me. I had to set the Build Action to Page and then change something in the XAML to get VS to recompile the page.
Brandon Montgomery
This was my problem, and it worked. also, props to Brandon for suggesting that the XAML needs recompiled.
thepaulpage