private void SetRootItem(object newValue) { if (RootItem != null) { RootItem.Owner = null; RootItem.ClearValue(ViewItem.ItemsSourceProperty); ItemsContainer.Children.Remove(RootItem); } if (ItemsContainer != null) { RootItem = new ViewItem() { Content = newValue, ContentTemplate = ItemTemplate, Owner = this }; if (ItemTemplate is HierarchicalDataTemplate) { var binding = (ItemTemplate as HierarchicalDataTemplate).ItemsSource; BindingOperations.SetBinding(RootItem, ViewItem.ItemsSourceProperty, binding); } ItemsContainer.Children.Add(RootItem); } }