Adding to a TreeView a ResourceDictionary from another Assembly
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
I managed to solve this bug. I removed to x:Key
from the HierarchicalDataTemplate
inside the ResourceDictionary
.
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>
精彩评论