Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件拖放到WPF中_C#_.net_Wpf_Image_Drag And Drop - Fatal编程技术网

C# 将文件拖放到WPF中

C# 将文件拖放到WPF中,c#,.net,wpf,image,drag-and-drop,C#,.net,Wpf,Image,Drag And Drop,我需要将一个图像文件放到我的WPF应用程序中。当前,当我将文件放入时,会触发一个事件,但我不知道下一步该怎么做。我如何获得图像?sender对象是图像还是控件 private void ImagePanel_Drop(object sender, DragEventArgs e) { //what next, dont know how to get the image object, can I get the file path here? } 图像文件包含在e参数中,该参数是的一

我需要将一个图像文件放到我的WPF应用程序中。当前,当我将文件放入时,会触发一个事件,但我不知道下一步该怎么做。我如何获得图像?
sender
对象是图像还是控件

private void ImagePanel_Drop(object sender, DragEventArgs e)
{
    //what next, dont know how to get the image object, can I get the file path here?
}

图像文件包含在
e
参数中,该参数是的一个实例。
(sender参数包含对引发事件的对象的引用。)

具体来说,检查;正如文档所解释的,这将返回对包含拖动事件数据的数据对象()的引用

IDataObject
接口提供了许多方法来检索您要查找的数据对象。您可能希望首先调用,以了解正在处理的数据的格式。(例如,它是实际图像还是仅仅是图像文件的路径?)


然后,一旦确定了要拖入的文件的格式,您将调用
GetData
方法的一个特定重载来实际检索特定格式的数据对象。

这基本上就是您想要做的

private void ImagePanel_Drop(object sender, DragEventArgs e)
{

  if (e.Data.GetDataPresent(DataFormats.FileDrop))
  {
    // Note that you can have more than one file.
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    // Assuming you have one file that you care about, pass it off to whatever
    // handling code you have defined.
    HandleFileOpen(files[0]);
  }
}
另外,不要忘记在XAML中实际连接事件,以及设置
AllowDrop
属性

<StackPanel Name="ImagePanel" Drop="ImagePanel_Drop" AllowDrop="true">
    ...
</StackPanel>

...

此外,回答A.R.请注意,如果您想使用
文本框
删除,您必须了解以下内容

TextBox
似乎已经对
dragandrop
进行了一些默认处理。如果您的数据对象是
字符串
,它就可以简单地工作。其他类型不会被处理,您将获得禁止鼠标效果,并且您的放置处理程序永远不会被调用

PreviewDragOver
事件处理程序中,似乎可以使用
e.Handled
启用自己的处理

XAML

<TextBox AllowDrop="True"    x:Name="RtbInputFile"      HorizontalAlignment="Stretch"   HorizontalScrollBarVisibility="Visible"  VerticalScrollBarVisibility="Visible" />

awesome很有魅力,只是将“HandleFileOpen(文件[0]);”替换为“foreach(文件中的字符串文件){Openfile(文件);}”-谢谢:)对不起:)我的意思是拖放不起作用。…
AllowDrop
设置为True,但从不调用
Drop
事件处理程序。当我将文件拖到窗口上时,我看到一个“拒绝”圆形符号,它使用
网格作为根元素,内部有
边框
,并将
背景属性设置为某个值(白色可以,但不透明)。在
边框内
我放置了实际的内容。当我试图放置到网格上时,将背景设置为透明对我来说效果很好。显然,你需要一个背景,这样才能进行命中测试。多亏了这篇博文:一个真正的问题是,如果您以管理员身份运行VisualStudio—调试您的应用程序—并以非管理员身份从FileExplorer拖动,则安全上下文不同,不会触发任何拖动事件。花费了我30分钟的生命。A.R.的例子忽略了PreviewDragOver处理程序,这对于将所有内容整合在一起非常重要。荣誉
RtbInputFile.Drop += RtbInputFile_Drop;            
RtbInputFile.PreviewDragOver += RtbInputFile_PreviewDragOver;

private void RtbInputFile_PreviewDragOver(object sender, DragEventArgs e)
{
    e.Handled = true;
}

private void RtbInputFile_Drop(object sender, DragEventArgs e)
{
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
                // Note that you can have more than one file.
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                var file = files[0];                
                HandleFile(file);  
     }
}