如何克隆 WPF 对象? [英] How can you clone a WPF object?

查看:26
本文介绍了如何克隆 WPF 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个很好的例子,如何深度克隆 WPF 对象,保留数据绑定?

Anybody have a good example how to deep clone a WPF object, preserving databindings?

标记的答案是第一部分.

The marked answer is the first part.

第二部分是您必须创建一个 ExpressionConverter 并将其注入序列化过程中.详细信息在这里:
http://www.codeproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2801571

The second part is that you have to create an ExpressionConverter and inject it into the serialization process. Details for this are here:
http://www.codeproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2801571

推荐答案

我所做的最简单的方法是使用 XamlWriter 将 WPF 对象保存为字符串.Save 方法将序列化逻辑树中的对象及其所有子项.现在您可以创建一个新对象并使用 XamlReader 加载它.

The simplest way that I've done it is to use a XamlWriter to save the WPF object as a string. The Save method will serialize the object and all of its children in the logical tree. Now you can create a new object and load it with a XamlReader.

例如:将对象写入 xaml(假设该对象是一个 Grid 控件):

ex: Write the object to xaml (let's say the object was a Grid control):

string gridXaml = XamlWriter.Save(myGrid);

将其加载到新对象中:

StringReader stringReader = new StringReader(gridXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid newGrid = (Grid)XamlReader.Load(xmlReader);

这篇关于如何克隆 WPF 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆