【问题标题】:Drag and drop files into WPF将文件拖放到 WPF 中
【发布时间】:2011-08-05 11:20:04
【问题描述】:

我需要将一个图像文件拖放到我的 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?
}

【问题讨论】:

    标签: c# .net wpf image drag-and-drop


    【解决方案1】:

    这基本上就是你想要做的。

    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>
    

    【讨论】:

    • awesome 很有魅力,只是换了“HandleFileOpen(files[0]);”到“foreach(文件中的字符串文件){ Openfile(文件);}” - 谢谢 :)
    • 对不起 :) 我的意思是拖放不起作用。 AllowDrop 设置为 True 但永远不会调用 Drop 事件处理程序。当我将文件拖到窗口上时,我看到一个“拒绝”圆形符号
    • 我使用了Grid 作为根元素,在内部使用Border 并将Background 属性设置为某个值(白色很好,但不透明)。在Border里面我放了实际的内容。
    • 将背景设置为透明对我来说在尝试拖放到网格时效果很好。显然你需要一个背景才能进行命中测试。感谢这篇博文:codeinreview.com/136/enabling-drag-and-drop-over-a-grid-in-wpf
    • 一个真正的问题是,如果您以管理员身份运行 VisualStudio - 调试您的应用程序 - 并以非管理员身份从 FileExplorer 拖动,安全上下文是不同的,并且不会触发拖动事件。花了我 30 分钟的生命。
    【解决方案2】:

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

    具体看e.Data member;正如文档所解释的,这将返回对包含拖动事件数据的数据对象 (IDataObject) 的引用。

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

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

    【讨论】:

      【解决方案3】:

      另外回答 A.R.请注意,如果您想使用TextBox 删除,您必须了解以下内容。

      TextBox 似乎已经对DragAndDrop 进行了一些默认处理。如果您的数据对象是String,它就可以正常工作。不处理其他类型,您会获得 禁止鼠标效果,并且您的 Drop 处理程序永远不会被调用。

      您似乎可以在 PreviewDragOver 事件处理程序中使用 e.Handledtrue 启用您自己的处理。

      XAML

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

      C#

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

      【讨论】:

      • A.R. 的示例错过了 PreviewDragOver 处理程序,这对于将它们组合在一起非常重要。赞一个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2013-04-21
      • 2011-10-21
      • 1970-01-01
      • 2011-06-19
      相关资源
      最近更新 更多