【问题标题】:WPF Drag&Drop is always enabled if a dialog box is open如果对话框打开,则始终启用 WPF 拖放
【发布时间】:2011-09-15 10:56:21
【问题描述】:

在我的 WPF 应用程序中,我实现了拖放以从 Windows 资源管理器打开文件 使用

  • AllowDrop="True"
  • DragOver="MainWindow_DragOver"
  • Drop="MainWindow_Drop"

没问题,一切正常。

但是如果一个对话框是打开的 我总是可以将文件从 Windows 资源管理器拖放到我的应用程序的主窗口中。 你有没有遇到过这个问题?你能帮我解决吗?谢谢

【问题讨论】:

    标签: wpf drag-and-drop


    【解决方案1】:

    如果您的对话框是模态对话框,它会禁用父窗口。这可以使用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

    【讨论】:

    • 这对我有用。 MS-Windows 允许在禁用的窗口上拖放的坏事......知道如何在整个应用程序中自动化它而不检查每个拖动功能中的启用状态吗?
    【解决方案2】:

    当对话框打开时,禁止拖放到您的主窗体上。确保打开一个模式对话框(ShowDialog 而不是 Show)。

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 2011-09-15
      • 2018-06-18
      • 2015-12-04
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多