拖放文件到WPF拖放、文件、WPF

由网友(慢热)分享简介:我需要删除的图像文件到我的WPF应用程序。目前,我有,当我放下了文件的事件触发,但我不知道该怎么下一步该怎么做。我如何获得的图像?是发件人对象的图像或控制?私人无效ImagePanel_Drop(对象发件人,DragEventArgs E){//何去何从,不知道如何获取图像的对象,我可以在这里得到的文件路径?}解决...

我需要删除的图像文件到我的WPF应用程序。目前,我有,当我放下了文件的事件触发,但我不知道该怎么下一步该怎么做。我如何获得的图像?是发件人对象的图像或控制?

 私人无效ImagePanel_Drop(对象发件人,DragEventArgs E)
{
    //何去何从,不知道如何获取图像的对象,我可以在这里得到的文件路径?
}
 

解决方案

这基本上是你想要做什么。

 私人无效ImagePanel_Drop(对象发件人,DragEventArgs E)
{

  如果(e.Data.GetData present(DataFormats.FileDrop))
  {
    //需要注意的是,你可以有多个文件。
    字符串[]文件=(字符串[])e.Data.GetData(DataFormats.FileDrop);

    //假设你有你关心一个文件,将其传递给什么
    //处理code你已经定义。
    HandleFileOpen(文件[0]);
  }
}
 
WPF开发之dll文件创建与调用

另外,不要忘了真正挂钩在XAML事件,以及设置的AllowDrop 属性。

 < StackPanel的名称=ImagePanel删除=ImagePanel_Drop的AllowDrop =真正的>
    ...
< / StackPanel的>
 

I need to drop an image file into my WPF application. I currently have a event firing when I drop the files in, but I don't know how what to do next. How do I get the Image? Is the sender object the image or the control?

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?
}

解决方案

This is basically what you want to do.

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

Also, don't forget to actually hook up the event in XAML, as well as setting the AllowDrop attribute.

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

阅读全文

相关推荐

最新文章