简体   繁体   English

如何正确允许WPF中的Drag n Drop?

[英]How do I properly allow Drag n Drop in WPF?

So I was working on this application in WinForm a year or so ago and decided to recreate it in WPF to make it more.. Up to date I guess. 因此,大约一年前,我在WinForm上开发了该应用程序,并决定在WPF中重新创建它,以使其功能更多。.我想是最新的。 And I just ran into my first problem, allowing drag n drop. 而我刚遇到第一个问题,允许拖动n下降。 When I try to drag and drop a file it wont work because it gives me that black circle with a line going through it. 当我尝试拖放文件时,它将无法正常工作,因为它给了我黑色的圆圈,并有一条直线穿过它。

This is what I've tried so far. 到目前为止,这是我尝试过的。 Setting AllowDrop = true. 设置AllowDrop = true。

Testing a few solutions on Stack 在堆栈上测试一些解决方案

Tested this one http://codeinreview.com/136/enabling-drag-and-drop-over-a-grid-in-wpf/ 测试了这个http://codeinreview.com/136/enabling-drag-and-drop-over-a-grid-in-wpf/

But how much I Google I cant find an answer, so I'm asking the pros. 但是我有多少Google找不到答案,所以我要问专业人士。 Lay it on me what I am doing wrong and how would I do this in a proper way? 把我做错的事放在我身上,我将如何以适当的方式做到这一点? This is the code I have so far 这是我到目前为止的代码

<Grid AllowDrop="True" Drop="Grid_Drop" DragOver="Grid_DragOver">
    <TextBox Name="tbSomething" Text="stuff" Margin="181,140,183,152"></TextBox>
</Grid>

    private void Grid_Drop(object sender, DragEventArgs e)
    {
        if (null != e.Data && e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            var data = e.Data.GetData(DataFormats.FileDrop) as string[];
            // handle the files here!
        }
    }

    private void Grid_DragOver(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.Copy;
        }
        else
        {
            e.Effects = DragDropEffects.None;
        }
    }

The problem is that TextBox has it's own drag and drop handlers which do not support FileDrop, which is why you get the Drag.None icon. 问题在于TextBox有它自己的拖放处理程序,该处理程序不支持FileDrop,这就是为什么要获得Drag.None图标的原因。

So you need to put your handler in front of the TextBox handler. 因此,您需要将处理程序放在TextBox处理程序的前面。 Like this... 像这样...

    <Grid>
        <TextBox Name="tbSomething" Text="stuff" Margin="20"  AllowDrop="True" PreviewDrop="Grid_Drop" PreviewDragOver="Grid_DragOver" ></TextBox>
    </Grid>

The preview handlers get called before the text box handlers. 预览处理程序在文本框处理程序之前被调用。

Then in the handlers... 然后在处理程序中...

    private void Grid_Drop(object sender, DragEventArgs e)
    {
        if (null != e.Data && e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            var data = e.Data.GetData(DataFormats.FileDrop) as string[];
            e.Handled = true;
            // handle the files here!
        }
    }

    private void Grid_DragOver(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.Copy;
            e.Handled = true;
        }
        else
        {
            e.Effects = DragDropEffects.None;
        }
    }

Note the e.Handled = true statements! 注意e.Handled = true语句! Without these the textbox handlers will simply overwrite your work. 没有这些,文本框处理程序将简单地覆盖您的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM