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);

 

posted @ 2015-11-19 18:00  Lyghost  阅读(3383)  评论(0编辑  收藏  举报