Adding a ResourceDictionary to a TreeView from another assembly
I have a TreeView item that I am trying to set to DataTemplates from a resource dictionary that is defined in another assembly. I use a fairly simple 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. It doesn't seem to work. I debugged it and noticed the ResourceDictionary is loaded. Please help me understand what I am missing. 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>
Thank,
Izhgar Lotem
+1
source to share
2 answers
I was actually trying to do something similar until I found a solution. From your code, I believe that the assembly containing the resources you are trying to load \ set is called "CarsLib.dll" or at least the assembly is internally called "CarsLib". However, I believe your code should look like this:
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>
0
source to share