WPF xmal中动态的复制粘贴控件或元素

前端xaml

<grid>

  <stackpanel x:name = "sp1">

    <button Content="复制粘贴" click = "button_click"/>

    <stackpanel orientation="horizontal" name = "Sp2">

    <textblock text = "测试"/>

    <textbox text = " 测试内容"/>

    </stackpanel>

  </stackpanel>

</grid>

 

后台代码

private void button_click(object sender,routedeventargs e)

{

  string xamlstring = XamlWriter.Save(sp2);

  object element = XamlReader.Parse(xamlString);

  UIElement uIElement  = (UIEement)element;

  sp1.Children.add(uIElement);

 

  //sp1.Children.add((UIEement)XamlReader.Parse(XamlWriter.Save(sp2)));

}

posted @ 2021-01-12 16:04  fate_WPF  阅读(502)  评论(0编辑  收藏  举报