10

I have an excel AddIn which exposes IWin32Window as it's main UI. I want to show a WPF window that uses this as it's parent.

How do I go about doing that ?

Pacman
  • 2,183
  • 6
  • 39
  • 70

2 Answers2

8

You can use WindowInteropHelper to parent the WPF window appropriately:

var helper = new WindowInteropHelper(theWpfWindow);
helper.Owner = win32Window.Handle;

theWpfWindow.Show(); // This is now parented appropriately
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I have this: System.Windows.Forms.IWin32Window (note, it is NOT namespace). MainHostWindow host = new MainHostWindow(); var helper = new WindowInteropHelper(host); helper.Owner = this._mainWindow.Handle; host.Owner is NULL – Pacman Apr 04 '11 at 19:57
  • 1
    @Pacman: host.Owner will stay null - you just set the interop helper as I showed, and it should parent it appropriately. If you want it modal, use ShowDialog()... – Reed Copsey Apr 04 '11 at 20:10
  • What is I want to make the Owner Window Disabled: host.Owner.IsEnabled = false ? – Pacman Apr 04 '11 at 20:14
  • @Pacman: You're trying to disable Excel's main window? You won't be able to do that from an extension - better to just use ShowDialog() to open your window modally, which will prevent the user from interacting with the host until you close your window. – Reed Copsey Apr 04 '11 at 20:29
0

I think you need to use a WindowInteropHelper, like shown here: IWin32Window Owner For WPF Window

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298