Select a data template by item type programatically in WPF
- by Michael Stoll
Hi all,
unfortunately the WPF ToolbarTray does not support binding to a collection of ToolbarViewModels (Correct me, if I'm wrong). Thus I want to create the Toolbars programmatically. So there are two tasks, which I don't know how to do:
Select the data template based on the item type.
Instantiate the data template as toolbar
Both should do the same as WPF does, when we use ItemsControl with an enumerable content and empty template.
I used reflector to anaylse what WPF does. Task 1 is done by FrameworkElement.FindTemplateResourceInternal, but this is internal, and I couldn't find any public methods to acomplish the task. Of course one could enumerate all resources and match the data template data type property, but this seems sub-optimal.
Who know's how to acomplish these tasks?