views:

121

answers:

1

I'm writing a shared addin for Excel. It adds a CommandBarButton that when clicked opens a WPF window to collect some information from the user.

I wanted to keep the same WPF dialog in memory and reuse it so that if the user clicks the CommandBarButton again their previous values would still be there.

So I made a reference to my WPF dialog as a private member of my addin object that implements Extensibility.IDTExtensibility2.

I created the window during OnStartupComplete(), but for some reason when I run Excel the window immediately opens even though I never called ShowDialog() and when I do call ShowDialog() when the CommandBarButton is clicked to reopen the window it fails to load.

Does anyone know why this happens and what the correct way to handle this is?

Thanks very much for any help.

CODE UPDATE:

public void OnStartupComplete(ref System.Array custom) 
{ 
   MyDialog dlg = new MyDlg(); //This will open the dialog ?!?!
}

....

 public MyDialog()
 {
      InitializeComponent();
      Loaded += new RoutedEventHandler(OnLoaded);
 }

OnLoaded just wires up some event handlers for buttons and sets some ItemSources. Even if I comment it out it still open the window.

I have picked up on the fact that once a WPF window is closed it can't be reoped and this is by design. But why it opens automatically when constructed inside an excel addin is a mystery.

+2  A: 

I've been able to reproduce your problem. In the WPF designer, make sure that the form's Visbility property is set to Collapsed. If you have it as Visible, it will automatically show when the dialog is created.

code4life
Ahh cool! That does work. Thank you. That is pretty wierd behavior though, right? :)
It looks like the Visibility is subtly different from a Winform's Visible property. Man, WPF takes some getting used to...!
code4life