2010-06-18 248 views
0

我已经创建了一个自定义Treeview控件,并试图从同一个项目中的另一个页面访问此控件的HierarchicalDataTemplate资源。WPF在App.xaml中的全局资源

我的this.sceneTemplate属性是空的。我遍历了字典中的所有对象,它确实存在。

如何访问此模板?

感谢

的App.xaml

<Application x:Class="Foo.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="MainWindow.xaml" 
    DispatcherUnhandledException="App_DispatcherUnhandledException"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <!--Merge the global resource to the application--> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Resources/Global.xaml"/> 
       <ResourceDictionary Source="Resources/Scrollbar.xaml"/> 
       <ResourceDictionary Source="/Controls/TreeView/Themes/Generic.xaml"/> 
       <ResourceDictionary Source="/Controls/ListButton/Themes/Generic.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 

    </Application.Resources> 
</Application> 

当我试图从我Scene.xaml.cs页

public LeagueDataTemplateSelector() 
{ 
    if (Application.Current != null) 
    { 

     this.sceneTemplate = (HierarchicalDataTemplate)Application.Current.FindResource("Scene"); 
     this.ItemTemplate = (HierarchicalDataTemplate)Application.Current.FindResource("Procedure"); 
     this.sub1Template = (HierarchicalDataTemplate)Application.Current.FindResource("SubProc1"); 
     this.sub2Template = (HierarchicalDataTemplate)Application.Current.FindResource("SubProc2"); 
    } 

} 

场景HierarchicalDataTemplate

<HierarchicalDataTemplate x:Key="Scene" ItemsSource="{Binding XPath=Level}" 
           ItemContainerStyle="{StaticResource RadTreeViewItemStyleSub1}" 
           ItemTemplate="{StaticResource Procedure}" 
           > 
     <Border> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Label VerticalContentAlignment="Center" 
         BorderThickness="0" 
         BorderBrush="Transparent" 
         Foreground="{StaticResource ListItemUnHighlight}" 
         FontSize="24" 
         Tag="{Binding [email protected]}" 
         MinHeight="55" 
         Cursor="Hand" 
         FontFamily="Arial" 
         KeyboardNavigation.TabNavigation="None" 
         Name="SubItem" > 
        <Label.ContextMenu> 
         <ContextMenu Name="editMenu"> 
          <MenuItem Header="Edit"/> 
         </ContextMenu> 
        </Label.ContextMenu> 
        <TextBlock Text="{Binding [email protected]}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock> 
       </Label> 
      </Grid> 
     </Border> 
    </HierarchicalDataTemplate> 
访问资源

更新 - 回答

我们的程序有一个通过使用页面列表来浏览的工作的面包屑控件。在运行InitializeComponent之前,在启动屏幕中添加了此页面列表。我将其移至页面列表代码上方,现在可以找到App.xaml中的所有内容。

+0

在应用程序启动事件中,是否可以遍历所有资源字典并查找其中的特定对象,以验证它是否加载了正确的字典? – decyclone 2010-06-18 19:12:47

+0

我已验证它位于正确的资源字典中。 – Ryan 2010-06-21 15:03:08

+0

@瑞恩,这将是值得发布您的更新作为一个实际的答案,然后在几天内接受它。这标志着“回答”这个问题。 – ChrisF 2010-06-22 20:10:12

回答

0

我们的程序有一个通过使用页面列表来浏览的面包屑控件。在运行InitializeComponent之前,在启动屏幕中添加了此页面列表。我将其移至页面列表代码上方,现在可以找到App.xaml中的所有内容。