0

I have a WPF TreeView bound to a hierarchy of objects of mixed types. Therefore, I use HierarchicalDataTemplates with the "DataType" property to specify the template used for each type, which works fine (the TreeView automatically catches the right one for each object type).

Now I have the problem that I have a second TreeView (in the same XAML file) which is to be populated with the same types of objects. This time, however, they have to be displayed in another way, so I need another set of HierarchicalDateTemplates. Again, I would use the "DataType" property. But if I put them in the same XAML file, of course I get an error (that I can't define two templates for the same type).

Is there a way to scope them somehow so the first set is exclusively used by the first TreeView, and the second one only by the second TreeView?

(Note that because the object tree structure is not known in advance, I have to use the "DataType" approach (which I prefer anyway) and can not use a static HierarchicalDataTemplate.)

juniper
  • 311
  • 5
  • 13

1 Answers1

1

Put the first DataTemplate set in the resources of your first TreeView, and the second one in the resources of your second TreeView and it should be working.

<TreeView>
    <TreeView.Resources>
        <!-- put your data templates here -->
    </TreeView.Resources>
</TreeView>

See FrameworkElement.Resources for more info.

Sisyphe
  • 4,626
  • 1
  • 25
  • 39
  • Hello Sisyphe, thank you very much for your help! I did not yet try your solution (I promise you to do this tomorrow), but I think that's it. In the meantime, another problem popped up; maybe you could have a look, of course only if you want to and can spare the time: http://stackoverflow.com/questions/13348405/factor-out-3rd-party-tab-with-docking-behaviour-to-its-own-xaml-file – juniper Nov 12 '12 at 17:34
  • Tried your solution and it works as expected. Many thanks for the prompt help. Do you have a short hint for the other problem I mentioned in the above comment? – juniper Nov 13 '12 at 10:02
  • Hey Juniper, glad to see this worked for you. I'll try to have a lookt at your other issue today. – Sisyphe Nov 13 '12 at 10:13