1
votes

In the constructor of a WPF Window I am setting its owner to be a WinForm using WindowInteropHelper. After setting this, the Owner property of the WPF Window is still null.

public WpfWindow(System.Windows.Forms.Form owner)
{
    if (owner != null)
    {
        var helper = new System.Windows.Interop.WindowInteropHelper(this);
        helper.Owner = owner.Handle;

        var Owner.Width; // Owner is null
     }
}

I need to get location information about the parent and hope to use Owner.Left, Owner.Width, etc. whether the owner is a WPF Window or a WinForm.

Is this possible? If not, what options do I have other than keeping a ref to the WinForm inside my WPF class?

1

1 Answers

2
votes

The Window.Owner property must be a Window (WPF) to be set.

The WindowInteropHelper allows you to set the window owner used for dialog placement, etc, but will not set the owner properties.

As you're doing this in the method directly, you can just use the owner parameter directly:

public WpfWindow(System.Windows.Forms.Form owner)
{
    if (owner != null)
    {
        int width = owner.Width; // Just use the parameter...
     }
}

Is this possible? If not, what options do I have other than keeping a ref to the WinForm inside my WPF class?

If you need to use this outside of the constructor, that is one option. Another option would be to keep the WindowInteropHelper around, and use P/Invoke on the HWND stored within the helper.Owner property to pull out the appropriate locations.