【问题标题】:Adding to a TreeView a ResourceDictionary from another Assembly将来自另一个程序集的 ResourceDictionary 添加到 TreeView
【发布时间】:2011-05-22 10:06:59
【问题描述】:

我有一个 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 如下所示:

<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>

谢谢,

伊扎尔洛特姆

【问题讨论】:

  • 请提供一些来自 TreeTemplateDictionary.xaml 的数据模板 xaml,你想用键或类型设置 TreeView 数据模板吗?

标签: wpf treeview resourcedictionary hierarchicaldatatemplate


【解决方案1】:

我设法解决了这个错误。我从ResourceDictionary 中的HierarchicalDataTemplate 删除到x:Key

【讨论】:

    【解决方案2】:

    在找到解决方案之前,我实际上一直在尝试做类似的事情。 从您的代码中,我相信包含您尝试加载\设置的资源的程序集称为“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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多