我想将树视图中存在的 xmldata 拖放到画布上,但无法得到它。我尝试了一些代码,可能会遗漏一些东西。下面是我的代码。
//handler for mouse move event
private void treeview_mousemove(object sender,MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DataObject data = new DataObject();
data.SetData(DataFormats.StringFormat, treeView1.SelectedItem.ToString());
data.SetData("string", treeView1.SelectedItem);
data.SetData("Object", this);
DragDrop.DoDragDrop(this,data, DragDropEffects.Copy);
}
}
//handler for drop event attached in canvas (Don`t know what to do here) getting
InvalidOperationException {Logical tree depth exceeded while traversing the tree. This could indicate a cycle in the tree.}
private void treeview_drop(object sender,DragEventArgs e)
{
e.Effects = DragDropEffects.None;
UIElement uieve = (UIElement)e.Data.GetData("Object");
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
canvas1.Children.Add(uieve);
e.Effects = DragDropEffects.Copy;
}
}
//handler for drag enter event in canvas
private void treeview_dragenter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
}
//handler for dragover event
private void treview_dragover(object sender,DragEventArgs e)
{
e.Effects = DragDropEffects.None;
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
string data = (string)e.Data.GetData(DataFormats.StringFormat);
e.Effects = DragDropEffects.Copy;
}
}
我想要的只是在画布中放置一个 xml 元素,但不知道该怎么做。提前致谢..