『WPF』實現拖動文件到窗體(控件)


前言

實現從窗口外部拖文件到窗口內部並自動捕獲文件地址。

第一步 開啟屬性

啟用底層WindowAllowDrop屬性,添加Drop事件。

Drop事件:當你拖動文件到對應控件后,松開觸發。

Drop事件外,我們還可以使用DragEnterDragOverDragLeave三個事件。

第二步 事件代碼

private void MainWindow_Drop(object sender, DragEventArgs e)
{
    string msg = "Drop";
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
    }

    MessageBox.Show(msg);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM