0

I have a TreeView with ItemsSource being nodes from different types.

class Node
class Folder : Node
class Project : Folder

All 3 classes are defined in the main project. And the xaml I currently use looks like this:

<TreeView Name="ProjectTreeView" SelectedItemChanged="ProjectTreeView_SelectedItemChanged">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type prj:Project}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type prj:Folder}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type prj:Node}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

What I want to do is have add-in system. In different assemblies I want to define types that will inherit Node or in some cases Folder. And I want these new types to have their own layout in the TreeView. Some of them may have multiple checkboxes or progress bar. The xaml for these new types should be part of the assemblies where the types are defined.

How can I at runtime tell the TreeView what template to use for these new Node types?

Aleks
  • 1,177
  • 10
  • 21
  • So, the new node types will be in different assemblies. Can you update the question to specify whether you'll define the templates in each node type's assembly OR within the current assembly? – Jake Berger Mar 09 '12 at 15:03
  • I tried making the question clearer. – Aleks Mar 09 '12 at 17:48
  • Did you consider instead of defining DataTemplates in Resources, injecting a DataTemplateSelector into treeview's ItemTemplateSelector, that you can update dynamically, when loading/unloading your addins? – Andreas H. Mar 11 '12 at 02:33

1 Answers1

0

In each add-in when you define each DataTemplate, ensure to specify the DataType.

In your main app, when you're importing the add-ins, you need to do Application.Current.Resources.Add templates from your add-ins. Then, when WPF sees a type, it will automatically 'inflate' using the defined DataTemplate.

Jake Berger
  • 5,237
  • 1
  • 28
  • 22