C#:WPF中的拖放(Richtextbox) [英] C#: Drag and Drop in WPF (Richtextbox)

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

问题描述

我想在WPF中实现一个拖放机制,但是它没有起作用。
使用Windows窗体工作,...



首先我将AllowDrop设置为True。在Windows窗体中,您可以将项目拖到富文本框中,游标更改。



使用WPF ....没有任何反应。



nexT点:实现DragEnter和DragDrop方法。
我像在线手册所说的那样做。 (好的,我不得不尝试更多的东西,因为DragDrop不存在于WPF)
我认为所有的拖放教程只适用于Windowsforms,没有WPF ...



richtextbox有问题吗?如果我将其更改为allowDrop - 没有任何反应。光标仍然是不允许的符号。



希望有人可以帮助:)



教程示例代码我读:

  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();
}


解决方案

我很好奇为什么所以我有一点玩,最终让它上班。基本上,我通过绑定到PreviewXXX事件,如下所示:

  AllowDrop =TruePreviewDragEnter =RichTextBox_DragEnterPreviewDragOver = RichTextBox_DragEnterPreviewDrop =RichTextBox_Drop

即使仍然,他们仍然没有工作。但答案来自此处,形式为提升的权限。我通常用管理员标高运行VS2010。因为我将资源管理器中的一些文件从我的 RichTextBox 中删除​​,所以基本上是禁止操作,因为资源管理器运行在非提升用户模式。在非升级模式下运行VS2010可以解决问题。链接的文章提示了一些解决方法,但我还没有尝试过。


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();
}

解决方案

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.

这篇关于C#:WPF中的拖放(Richtextbox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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