普通WPF窗口从一个Window1(Show方式)打开另一个Window2,只要设置Window2.Owner= Window1即可。这样Window2的显示和隐藏可由Window1自动控制。
但使用WPF UserControl Host到WinForm时,实现就难了。
刚才找到了解决方法:
Window win = new Window2();
HwndSource winformWindow =
(System.Windows.Interop.HwndSource.FromDependencyObject(this) as System.Windows.Interop.HwndSource);
if (winformWindow != null)
new WindowInteropHelper(win) { Owner = winformWindow.Handle };
win.Show();
下面是用户控件的弹出方式,用了导航窗口做载体
设置属性:
window = new NavigationWindow();
window.Title = title; //标题
window.Width = width;
window.Height = height;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.ResizeMode = ResizeMode.NoResize;
window.Source = new Uri(uri, UriKind.Relative);
window.ShowsNavigationUI = false; //去掉头部导航栏
window.Show();
网友评论