将另一个程序集中的 ResourceDictionary 添加到 TreeView
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法解决了这个错误。我从
ResourceDictionary
内的HierarchicalDataTemplate
中删除了x:Key
。I managed to solve this bug. I removed to
x:Key
from theHierarchicalDataTemplate
inside theResourceDictionary
.实际上我一直在尝试做类似的事情,直到找到解决方案。
从您的代码中,我相信包含您尝试加载\设置的资源的程序集称为“CarsLib.dll”,或者至少该程序集在内部称为“CarsLib”。
也就是说,我相信您的代码应该变成这样:
YourXamlWithTheTreeView.xaml
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