拖放文件到WPF [英] Drag and drop files into WPF
本文介绍了拖放文件到WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除的图像文件到我的WPF应用程序。目前,我有,当我放下了文件的事件触发,但我不知道该怎么下一步该怎么做。我如何获得的图像?是发件人
对象的图像或控制?
私人无效ImagePanel_Drop(对象发件人,DragEventArgs E)
{
//何去何从,不知道如何获取图像的对象,我可以在这里得到的文件路径?
}
解决方案
这基本上是你想要做什么。
私人无效ImagePanel_Drop(对象发件人,DragEventArgs E)
{
如果(e.Data.GetData present(DataFormats.FileDrop))
{
//需要注意的是,你可以有多个文件。
字符串[]文件=(字符串[])e.Data.GetData(DataFormats.FileDrop);
//假设你有你关心一个文件,将其传递给什么
//处理code你已经定义。
HandleFileOpen(文件[0]);
}
}
另外,不要忘了真正挂钩在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>
这篇关于拖放文件到WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文