1

我有一个 TreeView 元素,我试图从另一个程序集中定义的资源字典中设置它的 DataTemplates。我正在使用一种非常简单的方法:

<TreeView x:Name="treeView"
                  ItemsSource="{Binding Path=Vehicles}">
            <TreeView.Resources>                
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/CarsLib;component/TreeTemplateDictionary.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </TreeView.Resources>
        </TreeView>

然而。这似乎不起作用。我调试它并注意到 ResourceDictionary 已加载。请帮助我了解我错过了什么。资源字典看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:CarsLib">
<HierarchicalDataTemplate x:Key="StationTreeViewTemplate"
                          DataType="{x:Type local:Station}" 
                          ItemsSource="{Binding Path=FamounsModels}">
    <DockPanel>
        <TextBlock Text="{Binding Path=Name}" Margin="3,3,3,3" />
        <TextBlock Text="{Binding Path=EngineSize}" Margin="3,3,3,3" />
    </DockPanel>
</HierarchicalDataTemplate>

谢谢,

伊扎尔洛特姆

4

2 回答 2

4

我设法解决了这个错误。我 x:KeyHierarchicalDataTemplate里面取出来了ResourceDictionary

于 2011-05-23T13:47:56.877 回答
0

在找到解决方案之前,我实际上一直在尝试做类似的事情。从您的代码中,我相信包含您尝试加载\设置的资源的程序集称为“CarsLib.dll”,或者至少该程序集在内部称为“CarsLib”。也就是说,我相信你的代码应该变成这样:

YourXamlWithTheTreeView.xaml

<TreeView x:Name="treeView"
          ItemsSource="{Binding Path=Vehicles}">
    <TreeView.Resources>                
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Carslib;component/TreeTemplateDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </TreeView.Resources>
</TreeView>
于 2013-10-04T22:44:44.993 回答