<p>Using WPF, I have a TreeView control that I want to set its ItemTemplate dynamically through procedural code.  How do I do this?  I assume I need to find the resource somewhere.</p>

<pre><code>myTreeViewControl.ItemTemplate =   ??
</code></pre>

[{'answer_id': 31260, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 5, 'selected': True, 'text': '<p>If the template is defined in your &lt;Window.Resources&gt; section directly:</p>\n\n<pre><code>myTreeViewControl.ItemTemplate = this.Resources["SomeTemplate"] as DataTemplate;\n</code></pre>\n\n<p>If it\'s somewhere deep within your window, like in a &lt;Grid.Resources&gt; section or something, I think this\'ll work:</p>\n\n<pre><code>myTreeViewControl.ItemTemplate = this.FindResource("SomeTemplate") as DataTemplate;\n</code></pre>\n\n<p>And if it\'s elsewhere in your application, I <em>think</em> App.FindResource("SomeTemplate") will work.</p>\n'}, {'answer_id': 69365, 'author': 'Greg', 'author_id': 11013, 'author_profile': 'https://Stackoverflow.com/users/11013', 'pm_score': 2, 'selected': False, 'text': "<p>if your treeview control requires different templates for your items, you should implement DataTemplateSelector class and set it's instance to your tree view. as far as i remember there is a property of DataTemplateSelector.</p>\n"}]
['https://Stackoverflow.com/questions/31249', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3047/']