如何剪切,复制和粘贴扩展的树节点? [英] How do you cut, copy, and paste an extended Tree Node?
问题描述
我有一个c#项目使用版本4.0的.net框架,并运行在VS 2010.我创建了一个树视图填充一些扩展的树节点。我希望用户能够通过上下文菜单或键盘快捷键(而不只是拖放)将这些节点复制,剪切和粘贴到剪贴板。
I have a c# project using version 4.0 of the .net framework, and running on VS 2010. I have created a tree view populated with some extended tree nodes. I want the user to be able to copy, cut, and paste these nodes to the clipboard via a context menu or keyboard shortcuts (and not just drag-drop).
代码在复制时运行良好,但是当我尝试粘贴这些节点时,它会抛出这个错误:
无法转换类型为System.IO.MemoryStream的对象来键入Namespace Path.TreeNodeEx。
The code runs fine when copying, but when I try to paste these nodes it throws this error: Unable to cast object of type 'System.IO.MemoryStream' to type 'Namespace Path.TreeNodeEx'.
这是我的剪切/复制/粘贴方法。
Here's my cut/copy/paste methods.
public void Copy()
{
Clipboard.SetData("Tree Node Ex", CurrentTreeNode.Clone());
}
public void Paste()
{
CurrentTreeNode.Nodes.Add((TreeNodeEx)Clipboard.GetData("Tree Node Ex"));
}
我怀疑这个问题与序列化有关,
I suspect the problem is something to do with serialization, but I've tried implement the ISeralizable Interface and the [Serializable] attribute to no avail.
有没有建议?
推荐答案
事实证明,附加到每个扩展树节点我有一个字典存储额外的信息。显然,你不能序列化字典,所以这阻止了任何树节点序列化。
It turns out that attached to each extended tree node I had a dictionary that stored extra information. Apparently you can't serialize dictionaries, so this was preventing any of the tree nodes from being serialized.
我为这些扩展树节点实现了 ISerializable
,然后将字典转换为两个列表,回到反序列化构造函数中的字典。
I implemented ISerializable
for these extended tree nodes, and then converted the dictionary into two lists, which I then converted back to a dictionary in the deserialize constructor.
这篇关于如何剪切,复制和粘贴扩展的树节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!