WPF file dragging and dropping

WPF file dragging and dropping

When uploading a file, an upload button is generally provided. Click Upload to pop up the file (or directory selection dialog box). After selecting the file (or directory), the file path is obtained from the dialog box object, and then uploaded.

最后更新 11/27/2020 11:10 AM
沙漠尽头的狼
预计阅读 3 分钟
分类
WPF
标签
.NET WPF Document pulling

1. Before this article begins

When uploading a file, an upload button is generally provided. Click Upload to pop up the file (or directory selection dialog box). After selecting the file (or directory), the file path is obtained from the dialog box object, and then uploaded.

dialog box to select file

对话框选择文件

The selection dialog box code is as follows:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择Exe文件";
openFileDialog.Filter = "exe文件|*.exe";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = "exe";
if (openFileDialog.ShowDialog() == false)
{
    return;
}
string txtFile = openFileDialog.FileName;

But generally speaking, the best way for the user experience should be to directly drag and drop files with the mouse:

Baidu online disk drag-and-drop upload files

百度网盘拖拽上传文件

Here is a brief talk about the implementation of file drag-and-drop in WPF.

2. How to drag and drop files in WPF?

其实很简单,只要拖拽接受控件(或容器)注册这两个事件即可:DragEnterDrop

Let's first take a look at my implementation effect:

Drag and drop files into QuickApp

拖拽文件进QuickApp中

Registration events in Xaml

Registration event:

<Grid
  MouseMove="Grid_MouseMove"
  AllowDrop="True"
  Drop="Grid_Drop"
  DragEnter="Grid_DragEnter"
></Grid>

Event handling method:

  1. Grid_DragEnter processing method
private void Grid_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effects = DragDropEffects.Link;
    }
    else
    {
        e.Effects = DragDropEffects.None;
    }
}

DragDropEffects.Link: Handling drag-and-drop file operations

  1. Grid_Drop processing method

这是处理实际拖拽操作的方法,得到拖拽的文件路径(如果是操作系统文件快捷方式(扩展名为 lnk),则需要使用 com 组件(不是本文讲解重点,具体看本文开源项目)取得实际文件路径)后,即可处理后续操作(比如文件上传)。

private void Grid_Drop(object sender, DragEventArgs e)
{
    try
    {
        var fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        MenuItemInfo menuItem = new MenuItemInfo() { FilePath = fileName };

        // 快捷方式需要获取目标文件路径
        if (fileName.ToLower().EndsWith("lnk"))
        {
            WshShell shell = new WshShell();
            IWshShortcut wshShortcut = (IWshShortcut)shell.CreateShortcut(fileName);
            menuItem.FilePath = wshShortcut.TargetPath;
        }
        ImageSource imageSource = SystemIcon.GetImageSource(true, menuItem.FilePath);
        System.IO.FileInfo file = new System.IO.FileInfo(fileName);
        if (string.IsNullOrWhiteSpace(file.Extension))
        {
            menuItem.Name = file.Name;
        }
        else
        {
            menuItem.Name = file.Name.Substring(0, file.Name.Length - file.Extension.Length);
        }
        menuItem.Type = MenuItemType.Exe;

        if (ConfigHelper.AddNewMenuItem(menuItem))
        {
            AddNewMenuItem(menuItem);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

3. This article Over

The function is very simple, not deep, as long as you know how to use it.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读