WPF 实现 从窗体外拖动一文件 显示文件的 路径(地址)

1.启动Window的AllowDrop属性;即AllowDrop="True"。

2.添加Drop事件;即Drop="Window_Drop",当然事件名可以不用默认的而是用自定义的。PS:此处,除Drop事件外,我们还可以使用DragEnterDragOverDragLeave三个事件。

3.后台加事件的具体代码(见代码段)。

1 <Window x:Class="拖动文件到窗口显示文件路径.MainWindow"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         Title="MainWindow" Height="350" Width="525"
5         AllowDrop="True"
6         Drop="Window_Drop">
7     <Grid>        
8     </Grid>
9 </Window>
XAML
1 private void Window_Drop(object sender, DragEventArgs e)
2         {
3             string msg = "Drop";
4             if (e.Data.GetDataPresent(DataFormats.FileDrop))
5             {
6                 msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
7             }
8             MessageBox.Show(msg);
9         }
.cs
原文地址:https://www.cnblogs.com/zhangyongheng/p/4155448.html