将另一个程序集中的 ResourceDictionary 添加到 TreeView

发布于 2024-11-09 03:41:56 字数 1442 浏览 0 评论 0原文

我有一个 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>

谢谢,

伊扎尔·洛特姆

I have a TreeView element which I'm trying to sets its DataTemplates from a resource dictionary which is defined in another Assembly. I'm using quite a simplate approach:

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

However. This does not seems to work. I debugged it and noticed that the ResourceDictionary was loaded. Please help me understand what am I missing. The ResourceDictionary looks like this:

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

Thanks,

Izhar Lotem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-11-16 03:41:56

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

I managed to solve this bug. I removed to x:Key from the HierarchicalDataTemplate inside the ResourceDictionary.

久夏青 2024-11-16 03:41:56

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

I was actually trying to do something like that until I found a solution.
From your code I believe that the assembly that contains the resources you are trying to load\set is called "CarsLib.dll" or at least the Assembly is internally called "CarsLib".
That said, I believe that your code should become like:

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文