//This event handler is called when a child element of the ObservableCanvas //has a property that changes. void CanvasSource_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { ObservableCanvas source = sender as ObservableCanvas; if (source == null) { return; } CopyElements(source); }
//This event handler is called when a child is added to or removed from //the ObservableCanvas. private void CanvasSource_VisualChildrenChanged(object sender, RoutedEventArgs e) { ObservableCanvas source = sender as ObservableCanvas; if (source == null) { return; } CopyElements(source); }
//This function creates a brand new copy of the ObservableCanvas's //children and puts it into the target Canvas. private void CopyElements(ObservableCanvas source) { if (CanvasTarget == null) { return; } CanvasTarget.Children.Clear(); //Start from scratch. foreach (UIElement child in source.Children) { //We need to create a deep clone of the elements to they copy. //This is necessary since we can't add the same child to two different //UIlements. UIElement clone = (UIElement)XamlReader.Parse(XamlWriter.Save(child)); CanvasTarget.Children.Add(clone); } }