我有以下的XAML代码:
<controls:TabControl x:Name="workspacesTabControl" Grid.Row="1"
Background="AntiqueWhite" ItemsSource="{Binding Workspaces, ElementName=_root}"/>
然而,在代码后台我看不到workspacesTabControl
。我想也许是IntelliSense太刻薄了,于是试着编译它,但是出现了一个错误:
Error 1 The name 'workspacesTabControl' does not exist in the current context
如何在代码后台访问控件?
编辑:我意识到我复制了错误的内容 - 在UserControl中有两个名为workspacesTabControl和menuStrip的控件。我无法通过它们的名称在代码后台访问任何一个。
以防万一,这里是menuStrip
的XAML:
<controls:TreeView Grid.ColumnSpan="2" Height="100" x:Name="menuStrip"
ItemContainerStyle="{StaticResource MenuStripStyle}"
ItemsSource="{Binding Menu, ElementName=_root}"/>
再次编辑:
我不确定这是否有帮助,但我已经查看了InitializeComponent()
代码,以下是我看到的内容:
[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));
}
似乎它只是在运行时加载XAML(不是在编译期间或之前),因此
menuStrip
和workspacesTabControl
名称实际上没有在任何地方注册(通常在WPF/win Forms中会注册)。这个属性可能是一个问题吗?我在哪里可以摆脱这个要求,以便所有未来的用户控件都可以使用?