在WPF中检测Drag’n’Drop文件?

是否有可能让WPF窗口/元素检测从C#.Net 3.5中的Windows资源管理器拖拽文件? 我找到了WinForms的解决方案,但没有找到WPF的解决方案。

遗憾的是,TextBox,RichTextBox和FlowDocument查看器始终将拖放事件标记为已处理,从而防止它们冒泡到处理程序。 您可以通过强制处理拖放事件(使用UIElement.AddHandler并将processedEventsToo设置为true)并在处理程序中将e.Handled设置为false来恢复这些控件拦截的拖放事件。

请尝试以下方法:

private void MessageTextBox_Drop(object sender, DragEventArgs e) { if (e.Data is DataObject && ((DataObject)e.Data).ContainsFileDropList()) { foreach (string filePath in ((DataObject)e.Data).GetFileDropList()) { // Processing here } } } private void MessageTextBox_PreviewDragEnter(object sender, DragEventArgs e) { var dropPossible = e.Data != null && ((DataObject)e.Data).ContainsFileDropList(); if (dropPossible) { e.Effects = DragDropEffects.Copy; } } private void MessageTextBox_PreviewDragOver(object sender, DragEventArgs e) { e.Handled = true; } 

事实certificate我出于某种原因无法放入我的TextBox,但是按下按钮工作正常。 通过将’AllowDrop =“True”’添加到我的窗口并将drop事件处理程序添加到包含以下内容的按钮来实现它:

 private void btnFindType_Drop(object sender, DragEventArgs e) { if (e.Data is System.Windows.DataObject && ((System.Windows.DataObject)e.Data).ContainsFileDropList()) { foreach (string filePath in ((System.Windows.DataObject)e.Data).GetFileDropList()) { // Processing here } } } 

我注意到WPF中的拖放并不是那么容易。 所以我写了一篇关于这个主题的简短文章: http : //www.wpftutorial.net/DragAndDrop.html

我有类似的问题,drop事件和拖动输入事件没有被触发。 问题出在Windows用户帐户设置上。 将其设置为最不安全的设置并尝试使用相同的代码。