Note that there are some explanatory texts on larger screens.

plurals
  1. POPorting WinForms drag and drop to WPF drag and drop
    primarykey
    data
    text
    <p>I am porting my program from WinForms to WPF and have ran into some issues with the drag and drop. It should allow for dragging from a TreeView (it is like a file explorer) to a textbox which opens the file. However, the WPF version acts like a copy-and-paste of the <code>TreeViewItem</code>'s header text automatically. I think I just have something mixed up? Possibly the <code>DataObject</code> stuff.</p> <p>The fully functional, relevant WinForms code:</p> <pre><code>private void treeView1_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; TreeNode node = treeView1.GetNodeAt(e.Location); if (node != null) treeView1.DoDragDrop(node, DragDropEffects.Move); } textbox[i].DragDrop += (o, ee) =&gt; { if (ee.Data.GetDataPresent(typeof(TreeNode))) { TreeNode node = (TreeNode)ee.Data.GetData(typeof(TreeNode)); ((Textbox)o).Text = File.ReadAllLines(pathRoot + node.Parent.FullPath); ... </code></pre> <p>The WPF code that should do the same thing:</p> <pre><code>private void TreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { TreeViewItem item = e.Source as TreeViewItem; if (item != null) { DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.StringFormat, GetFullPath(item)); DragDrop.DoDragDrop(item, dataObject, DragDropEffects.Move); } } //textbox[i].PreviewDrop += textbox_Drop; private void textbox_Drop(object sender, DragEventArgs e) { TreeViewItem node = (TreeViewItem)e.Data.GetData(typeof(TreeViewItem)); //null? ((Textbox)sender).Text = ""; //this is being executed BUT then the node's header text is being pasted //also, how do I access the DataObject I passed? } </code></pre> <p><strong>Problem:</strong> In my WPF version, I am setting the textbox's text to empty (as a test), which occurs, but afterwards the TreeViewItem's header text is being pasted which is not what I want.</p> <p><strong>Questions:</strong> What is the correct way to port this WinForms code to WPF? Why is the text being pasted in the WPF version? How do I prevent that? Am I using the correct events? How do I access the <code>DataObject</code> in <code>textbox_Drop</code> so that I can open the file like I did in the WinForms version? Why is TreeViewItem node always null in the WPF version?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload