private static void OnContentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CachedContentControl control = ((CachedContentControl)d); //((dynamic)control.Content).DataContext = null; // use you favorite factory if (e.NewValue == null) { //control.Content = null; return; } if (e.OldValue == null || (e.OldValue != null && e.NewValue.GetType() != e.OldValue.GetType())) { FrameworkElement content = ViewFactory.GetView(e.NewValue.GetType()); control.Content = content; content.DataContext = control.DataContext; //((ContentControl)content).Content = control.DataContext; Binding binding = new Binding(); BindingOperations.SetBinding(content, ContentProperty, binding); } else { ((FrameworkElement)control.Content).DataContext = control.DataContext; } }
private void RefreshCachedControl(LayoutDocument document) { ScrollViewer sv = UITreeHelper.FindChild <ScrollViewer>(((dynamic)document.Content).Content, "contentSV"); if (sv != null) { CachedContentControl cachedContent = sv.Content as CachedContentControl; if (cachedContent.Content == null) { var tmp = MainVM.SelectedItem; MainVM.SelectedItem = null; MainVM.SelectedItem = tmp; } } }