【问题标题】:WPF Global Resources in App.xamlApp.xaml 中的 WPF 全局资源
【发布时间】:2010-06-18 13:47:50
【问题描述】:

我创建了一个自定义 Treeview 控件,并尝试从同一项目中的另一个页面访问此控件的 HierarchicalDataTemplate 资源。

我的 this.sceneTemplate 属性即将变为 null。我遍历了字典中的所有对象,它确实存在。

如何访问此模板?

谢谢

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 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 XPath=@name}" 
                        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 XPath=@name}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
                </Label>
            </Grid>
        </Border>
    </HierarchicalDataTemplate>

更新 - 回答

我们的程序有一个面包屑控件,它通过使用页面列表进行导航。此页面列表是在 InitializeComponent 运行之前的初始屏幕期间添加的。我将它移到页面列表代码上方,现在可以找到 App.xaml 中的所有内容。

【问题讨论】:

  • 在应用程序启动事件中,您能否遍历所有资源字典并在其中查找特定对象,以验证它是否加载了正确的字典?
  • 我已验证它位于正确的资源字典中。
  • @Ryan,值得将您的更新发布为实际答案,然后在几天内接受它。这将问题标记为“已回答”。

标签: wpf xaml resources


【解决方案1】:

我们的程序有一个面包屑控件,它通过使用页面列表进行导航。此页面列表是在 InitializeComponent 运行之前的初始屏幕期间添加的。我将它移到页面列表代码上方,现在可以找到 App.xaml 中的所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 2018-12-02
    • 2010-10-21
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多