views:

109

answers:

0

I've defined a HierarchicalDataTemplate in the App ResourceDictionary. If I set the ItemsSource property in XAML, the TreeView looks and behaves as expected. However, if I set the DataTemplate to this object in code, remove the ItemsSource value from XAML and instead set it in code (like the following example), it gets ignored and the tree nodes don't show any children. Why is this and how can I get around it?

var node = item as Node;
var template = App.Current.Resources["NodePresenterTemplate"] as HierarchicalDataTemplate;
template.ItemsSource = node.ChildNodes; // ignored in Silverlight, works in WPF
return template;