移植的WinForms拖放到WPF拖放 [英] Porting WinForms drag and drop to WPF drag and drop

查看:217
本文介绍了移植的WinForms拖放到WPF拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从的WinForms到WPF移植我的计划,并遇到了一些问题拖放。它应该允许从一个TreeView拖(它就像一个文件浏览器),以打开该文件的文本框。不过,WPF的版本就像一个复制和粘贴树型视图的标题文本自动。我想我只是有一些混了?可能是数据对象的东西。

全功能的,相关的WinForms code:

 私人无效treeView1_MouseMove(对象发件人,发送MouseEventArgs E)
{
    如果(e.Button = MouseButtons.Left!)回报;
    树节点节点= treeView1.GetNodeAt(e.Location);
    如果(节点!= NULL)treeView1.DoDragDrop(节点,DragDropEffects.Move);
}

文本框[I] .DragDrop + =(0,EE)=>
{
     如果(ee.Data.GetData present(typeof运算(树节点)))
     {
         树节点节点=(树节点)ee.Data.GetData(typeof运算(树节点));
         ((文本框)O)。文= File.ReadAllLines(pathRoot + node.Parent.FullPath);
         ...
 

在WPF code,应该做同样的事情:

 私人无效TreeView_ previewMouseLeftButtonDown(对象发件人,MouseButtonEventArgs E)
{
    树型视图项目= e.Source为树型视图;
    如果(项目!= NULL)
    {
        数据对象数据对象=新的数据对象();
        dataObject.SetData(DataFormats.StringFormat,GetFullPath(项目));
        DragDrop.DoDragDrop(项目,数据对象,DragDropEffects.Move);
    }
}

//textbox[i].$p$pviewDrop + = textbox_Drop;
私人无效textbox_Drop(对象发件人,DragEventArgs E)
{
    树型视图节点=(树型视图)e.Data.GetData(typeof运算(树型视图)); //空值?
    ((文本框)发件人)。文=;
    //这是正在执行但随后该节点的标题文本被粘贴
    //同时,该怎样访问我通过数据对象?
}
 

问题:在我的WPF的版本,我设置文本​​框的文本为空(作为测试),它发生,但事后TreeViewItem的标题文本被粘贴,这是不是我想要的。

问题:什么是正确的方式来口本的WinForms code到WPF?为什么被粘贴的文本在WPF版本?我如何prevent是什么?我使用了正确的事件?我如何访问数据对象 textbox_Drop 让像我在的WinForms版本中那样我可以打开该文件?为什么在WPF的版本是树型视图节点总是空?

解决方案
  

问题:在我的WPF的版本,我设置文本​​框的文本为空(作为测试),它发生,但事后TreeViewItem的标题文本被粘贴,这是不是我想要的。

我觉得父UI元素是处理(因此覆盖)的删除事件,使你没有得到你所期望的结果。作为事实上,试图重新您的问题的时候,我甚至不能让我的TextBox.Drop事件激发。但是,使用文本框的previewDrop事件,我能得到什么(我认为)是您预期的结果。试试这个:

 私人无效textBox1_ previewDrop(对象发件人,DragEventArgs E)
    {
        文本框TB =发件人的文本框;
        如果(TB!= NULL)
        {
            //如果数据对象包含字符串数据,将其解压缩。
            如果(e.Data.GetData present(DataFormats.StringFormat))
            {
                字符串文件名= e.Data.GetData(DataFormats.StringFormat)的字符串;
                使用(StreamReader的S = File.OpenText(文件名))
                {
                    ((文本框)发件人)。文= s.ReadToEnd();
                }
            }
        }
        e.Handled =真实; //一定要设置为true
    }
 

我认为,code段应回答你们大多数人都提出,除了这一个问题:

  

为什么在WPF的版本是树型视图节点总是空?

数据对象要传递的DragDrop事件不支持将一个树型视图。在您的code(和我),我们指定的数据格式将是 DataFormats.StringFormat 不能转换为树型视图

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 TreeViewItem's header text automatically. I think I just have something mixed up? Possibly the DataObject stuff.

The fully functional, relevant WinForms 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) =>
{
     if (ee.Data.GetDataPresent(typeof(TreeNode)))
     {
         TreeNode node = (TreeNode)ee.Data.GetData(typeof(TreeNode));   
         ((Textbox)o).Text = File.ReadAllLines(pathRoot + node.Parent.FullPath);
         ...

The WPF code that should do the same thing:

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?
}

Problem: 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.

Questions: 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 DataObject in textbox_Drop so that I can open the file like I did in the WinForms version? Why is TreeViewItem node always null in the WPF version?

解决方案

Problem: 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.

I think a parent UI element is handling (and therefore overriding) the Drop event so you're not getting the results you expect. As a matter of fact, when trying to recreate your issue, I couldn't even get my TextBox.Drop event to fire. However, using the TextBox's PreviewDrop event, I was able to get what (I think) is your expected result. Try this:

    private void textBox1_PreviewDrop(object sender, DragEventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb != null)
        {
            // If the DataObject contains string data, extract it.
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string fileName = e.Data.GetData(DataFormats.StringFormat) as string;
                using (StreamReader s = File.OpenText(fileName))
                {
                    ((TextBox)sender).Text = s.ReadToEnd();
                }
            }
        }
        e.Handled = true; //be sure to set this to true
    }

I think that code snippet should answer most of the questions you posed except for this one:

Why is TreeViewItem node always null in the WPF version?

The DataObject you are passing in the DragDrop event does not support passing a TreeViewItem. In your code (and mine) we specify that the data format will be DataFormats.StringFormat which cannot be cast to a TreeViewItem.

这篇关于移植的WinForms拖放到WPF拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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