wpf 控件复制 克隆
方法1:
1 string xaml = System.Windows.Markup.XamlWriter.Save(rtb1); 2 RichTextBox rtb2 =System.Windows.Markup.XamlReader.Parse(xaml) as RichTextBox;
方法2:
1 Rectangle rect = new Rectangle(); 2 rect = data.GetData(typeof(Rectangle)) as Rectangle; 3 //canvas2.Children.Remove(rect); 4 //canvas1.Children.Add(rect); 5 //序列化Control,以深复制Control!!!! 6 string rectXaml = XamlWriter.Save(rect); 7 StringReader stringReader = new StringReader(rectXaml); 8 XmlReader xmlReader = XmlReader.Create(stringReader); 9 UIElement clonedChild = (UIElement)XamlReader.Load(xmlReader); 10 canvas1.Children.Add(clonedChild);