我有一个使用.net框架4.0版的c#项目,并在VS 2010上运行。我创建了一个填充了一些扩展树节点的树视图。我希望用户能够通过上下文菜单或键盘快捷键(而不仅仅是拖放)将这些节点复制,剪切和粘贴到剪贴板。
复制时代码运行正常,但是当我尝试粘贴这些节点时会抛出此错误: 无法将“System.IO.MemoryStream”类型的对象强制转换为“Namespace Path.TreeNodeEx”。
这是我的剪切/复制/粘贴方法。
public void Copy()
{
Clipboard.SetData("Tree Node Ex", CurrentTreeNode.Clone());
}
public void Paste()
{
CurrentTreeNode.Nodes.Add((TreeNodeEx)Clipboard.GetData("Tree Node Ex"));
}
我怀疑这个问题与序列化有关,但我尝试实现ISeralizable接口和[Serializable]属性无济于事。
有什么建议吗?
答案 0 :(得分:1)
事实证明,附加到每个扩展树节点的我都有一个存储额外信息的字典。显然,您无法序列化字典,因此这阻止了任何树节点的序列化。
我为这些扩展树节点实现了ISerializable
,然后将字典转换为两个列表,然后我将其转换回deserialize构造函数中的字典。
答案 1 :(得分:0)
试试这个:
Clipboard.GetDataObject().GetData(typeof(TreeNodeEx))
答案 2 :(得分:0)
public void copy()
{
tempNode = TreeView.SelectedNode;
}
public void paste()
{
TreeView.SelectedNode.Nodes.Add(new TreeNode(tempNode.Text));
tNode = TreeView.SelectedNode.LastNode;
for (int i = 0; i <= tempNode.Nodes.Count - 1; i++)
{
TreeNode NodeClone = (TreeNode)tempNode.Nodes[i].Clone();
tNode.Nodes.Add(NodeClone);
}
public void cut()
{
tempNode = TreeView.SelectedNode;
TreeView.SelectedNode.Remove();
}
答案 3 :(得分:0)
旧的东西,但我花了几个小时面对同样的问题所以这是有效的:
Imports System.Runtime.Serialization
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.TV.Nodes.Clear()
Dim tNode As New TemplateNode()
Me.TV.Nodes.Add(tNode)
For i As Integer = 1 To 5
Dim newNode As New TemplateNode()
tNode.Nodes.Add(newNode)
tNode = newNode
Next
Me.TV.ExpandAll()
End Sub
Private Sub TV_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles TV.ItemDrag
DoDragDrop(Me.TV.SelectedNode, DragDropEffects.Copy + DragDropEffects.Move + DragDropEffects.Scroll)
End Sub
Private Sub TV_DragEnter(sender As Object, e As DragEventArgs) Handles TV.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub TV_DragOver(sender As Object, e As DragEventArgs) Handles TV.DragOver
e.Effect = DragDropEffects.Move
If (e.KeyState And 8) = 8 Then
e.Effect += DragDropEffects.Copy
End If
End Sub
Private Sub TV_DragDrop(sender As Object, e As DragEventArgs) Handles TV.DragDrop
Dim TemplateNode As TemplateNode = e.Data.GetData(GetType(TemplateNode))
Me.TV.Nodes.Add(TemplateNode.Clone())
End Sub
Private Sub bCopy_Click(sender As Object, e As EventArgs) Handles bCopy.Click
My.Computer.Clipboard.SetData("TemplateNode", Me.TV.SelectedNode)
End Sub
Private Sub bCut_Click(sender As Object, e As EventArgs) Handles bCut.Click
My.Computer.Clipboard.SetData("TemplateNode", Me.TV.SelectedNode)
Me.TV.SelectedNode.Remove()
End Sub
Private Sub bPaste_Click(sender As Object, e As EventArgs) Handles bPaste.Click
Dim TemplateNode As TemplateNode = My.Computer.Clipboard.GetData("TemplateNode")
If Me.TV.SelectedNode Is Nothing Then
Me.TV.Nodes.Add(TemplateNode)
Else
Me.TV.SelectedNode.Nodes.Add(TemplateNode)
End If
End Sub
End Class
<Serializable()> _
Public Class TemplateNode
Inherits TreeNode
Public MyString As String = "MyStringValue"
Public MyDateTime As DateTime = DateTime.Now
Public MyBytes() As Byte
Public Sub New()
Dim buff(255) As Byte
For i As Integer = 0 To buff.GetUpperBound(0)
buff(i) = CByte(Rnd() * 100)
Next
Me.MyBytes = buff
Me.Text = Guid.NewGuid().ToString()
End Sub
Protected Friend Sub New(info As SerializationInfo, context As StreamingContext)
MyBase.New(info, context)
End Sub
End Class