2010-06-18 105 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中的所有內容。