你怎么可以克隆一个WPF对象? [英] How can you clone a WPF object?

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

问题描述

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

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


被标记的答案是第一部分。

The marked answer is the first part.

第二部分是,你必须创建一个防爆pressionConverter并注入到序列化进程。这个细节在这里:

<一href=\"http://www.$c$cproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2801571\">http://www.$c$cproject.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);

加载到一个新的对象:

Load it into a new object:

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

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

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