C#: Drag and Drop in WPF (Richtextbox)

时间秒杀一切 提交于 2019-12-22 10:45:09

问题


i want to implement a Drag/Drop mechanic in WPF, but it didn't work... With Windows-Forms it worked, ...

First i set AllowDrop to True. In windows-forms you can already drag items into the richtextbox and the cursor changes.

With WPF .... nothing happens.

The nexT point: Implement DragEnter and DragDrop Methodes. I did it like the online-manuals says. (ok i had to try out something more, because DragDrop doesn't exists in WPF) I think all tutorials for drag/drop is only for Windowsforms, nothing for WPF...

Is there a problem with the richtextbox? If i change it to "allowDrop" - nothing happens. The cursor is still a not-allowed-symbol.

Hope someone can help :)

Examplecode from tutorials i read:

richTextBox1.AllowDrop = true;

void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.None;

    if (e.Data.GetDataPresent(DataFormats.XXX))
    {
        e.Effect = DragDropEffects.Copy;
    }
}

void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
    //intert in richtextbox ...
    richTextBox1.methodeXY();
}

回答1:


I was curious as to why so I had a little play and eventually got it to work. Essentially, I started by binding to the PreviewXXX events like so:

AllowDrop="True" PreviewDragEnter="RichTextBox_DragEnter" PreviewDragOver="RichTextBox_DragEnter" PreviewDrop="RichTextBox_Drop"

Even still, they STILL didn't work. But the answer came here, in the form of elevated permissions. I usually run VS2010 with admin elevation. Because I was dropping some files from Explorer onto my RichTextBox, it was essentially banning the operation because Explorer runs in a non-elevated user mode. Running the VS2010 in a non-elevated mode fixed the problem. The article linked does suggest some workarounds, but I haven't tried them yet.



来源:https://stackoverflow.com/questions/4418493/c-drag-and-drop-in-wpf-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!