在WPF应用程序中,我实现了拖放操作以从Windows资源管理器中打开文件
使用AllowDrop="True"
DragOver="MainWindow_DragOver"
Drop="MainWindow_Drop"
没问题,一切正常。
但是如果对话框打开
我始终可以将文件从Windows资源管理器拖放到应用程序的主窗口中。
您遇到过这个问题吗?你能帮我解决吗?谢谢
最佳答案
如果您的对话框是模式对话框,则会禁用父窗口。可以使用IsWindowEnabled API call进行检查。 (我不是WPF专家,但是.IsEnabled或.Focusable属性似乎无法以这种方式工作。)
void MainWindow_DragOver(object sender, DragEventArgs e)
{
bool isEnabled = NativeMethods.IsWindowEnabled(new WindowInteropHelper(this).Handle);
e.Effects = isEnabled ? DragDropEffects.Copy : DragDropEffects.None;
e.Handled = true;
}
class NativeMethods
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowEnabled(IntPtr hWnd);
}
另见https://stackoverflow.com/a/6363095/9156214
关于wpf - 如果打开对话框,则始终启用WPF拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7429685/