开发者

Is there a method like Winforms.Show ( IWin32Window owner ) in WPF?

I am creating a new WPF window that I want to开发者_运维知识库 be parented to another existing app. I do this with the:

Winforms.Show ( IWin32Window owner )

all the time, but since this app is using a WPF Window, I am looking for a way to achieve the same thing.

Any ideas?


The above way is valid to do this from a WPF parent window to a WPF child window

Window wpfWindow = new Window(); 
wpfWindow.Owner = this; 

If you want to have a WPF windows with a Winform parent you need to use the WindowInteropHelper class.

WindowInteropHelper helper = new WindowInteropHelper(wpfWindow);
helper.Owner = winFormWindow.Handle.


Yes, you can set the Owner property of the Window.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ShowChildWindow()
    {
        MyChildWindow window = new MyChildWindow();
        window.Owner = this; // Set owner of child window.
        window.Show();
    }
}


Ok I found the answer.

   MyWpfDialog dialog = new MyWpfDialog();

   //remember, this is WinForms UserControl and its Handle property is
   //actually IntPtr containing Win32 HWND.
   new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;
   dialog.ShowDialog();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜