Reputation: 7683
I implemented a simple WPF-Window to be loaded into a VSTO-Addin application for excel.
The context menù flashes, that is it get shown and suddently disappears; this only happens when the window is NOT modal.
Reproducing the problem is very simple; first you have to create an Excel 200X VSTO Add-in.
Add a WPF user control, change the root node from UserControl to Window. Change the code behind consistently, that is replace the superclass from UserControl to Window.
This is a trick to create a WPF Window because when you use a VSTO Add-in there is no WPF Window among project items. This is also the most likely culprit of the problem.
The window contains just a Label with a context menù.
The xaml:
<Window x:Class="ExcelAddIn9.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ExcelAddIn9"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<Label>Some label
<Label.ContextMenu>
<ContextMenu>
<MenuItem Header="Context Menu"/>
</ContextMenu>
</Label.ContextMenu>
</Label>
</StackPanel>
</Window>
When it works the output is the foollowing:
Just for show, you can open the window from anywhere in the ThisAddIn class.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MyWindow w = new MyWindow();
w.Show();
}
This flashes. To have it working change w.Show()
to w.ShowDialog()
. Again, that's not the place you put a dialog in a real application since it blocks the excel loading, but for showing the problem it's ok.
As I said, I think that this may be one of the reasons why they don't provide a WPF-Window item for a VSTO project, but the same I would like to investigate the issue in depth in order to have the ContextMenu work also in a non dialog application.
And I would like to check wether this problem is a symptom of a basic malfunctioning of WPF windows in VSTO and evaluating other soutions (maybe embedding them in WinForms windows).
VSTO Version 2010.
Upvotes: 0
Views: 156
Reputation: 49455
You must specify the parent Outlook window handle (Owner
). Use the WindowInteropHelper class which assists interoperation between Windows Presentation Foundation (WPF) and Win32 code.
An example scenario is if you need to host a WPF dialog box in a Win32 application. Initialize the WindowInteropHelper with a WPF window object for the dialog box. You can then get the WPF window's handle (HWND) from the Handle property and specify the owner for the WPF window with the Owner property. The following code example shows how to use WindowInteropHelper when hosting a WPF dialog box in a Win32 application.
WindowInteropHelper wih = new WindowInteropHelper(myDialog);
wih.Owner = ownerHwnd;
myDialog.Show();
Upvotes: 1