=========================== 开始更新 ===================== =================
代码:
using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
using System.Runtime.InteropServices; // DllImport
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();
// Get the handle to the non-WPF owner window
IntPtr hWnd = ...
CenteredWindow cw = new CenteredWindow();
EnableWindow(hWnd, false); // disable parent window
try
{
// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = hWnd;
cw.ShowDialog();
}
finally
{
EnableWindow(hWnd, true); // enable parent window
}
...
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool EnableWindow(IntPtr hwnd, bool enable);
在@kamal-nayan 评论中的MS Connect Link的帮助下,我修改了上面的代码,它对我来说效果很好。
关键是禁用父窗口,当你的模态对话框关闭时,启用父窗口。
============================ 结束更新===================== =================
using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();
// Get the handle to the non-WPF owner window
IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window
// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
cw.ShowDialog();
这是我找到的唯一解决方案。这不是真正的模态,即您仍然可以激活父窗口,但好在子窗口仍然在父窗口之上。
http://blogs.msdn.com/b/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx