Context menu on add-in

Jan 28, 2009 at 11:33 PM
Hi again,

I jump forward to a new problem...
I now have a bunch of UI add-ins, UserControls created using INativeHandleContract and so on...
Once they're activated, they're currently just added to a "main" grid of my host application...

What I want to do is for all my add-ins to have the same "pop-up menu" when I right click them (i.e. ContextMenu).
In the host I've created a simple ContextMenu, which I set to the add-in's ContextMenu property.
No errors, no exception, no ContextMenu... No nothing... It does not seem to have any effect...

If I runtime create a Button from code-behind, and set the ContextMenu to it, right clicking it gives me the menu...

This works (along with setting button width/height, text etc):
Button b = new Button();
b.ContextMenu = myContextMenu;
myGrid.Children.Add(b);
But this does not:
VisualAddIn vAddin = myToken.Activate<VisualAddIn>(AddInSecurityLevel.FullTrust);
vAddin.ContextMenu = myContextMenu;
myGrid.Children.Add(vAddin);

To make sure I didn't do anything wrong, I went back to basics:
  1. I downloaded the Tech Sample from MSDN Add-In Is a UI Sample
  2. To the MainWindow.xaml.cs, I added the following code (at line 44, between activation and adding the control to the grid)
    1. this.wpfAddInHostView.ContextMenu = MyMenu();
  3. I add added the following funtion to the MainWindow class:
System.Windows.Controls.ContextMenu MyMenu()
{
System.Windows.Controls.MenuItem m1, m2, m3, m4;
System.Windows.Controls.ContextMenu _contextMenu = new System.Windows.Controls.ContextMenu();
m1 = new System.Windows.Controls.MenuItem();
m1.Header = "File";
m2 = new System.Windows.Controls.MenuItem();
m2.Header = "Save";
m3 = new System.Windows.Controls.MenuItem();
m3.Header = "SaveAs";
m4 = new System.Windows.Controls.MenuItem();
m4.Header = "Recent Files";
_contextMenu.Items.Add(m1);
_contextMenu.Items.Add(m2);
_contextMenu.Items.Add(m3);
_contextMenu.Items.Add(m4);
return _contextMenu;
}
More simple than that is hard to get.
But, it does not work. What am I doing wrong?? Shouldn't the activated add-in, a UserControl, be as easy to handle as a standard Button??
Can anyone come up with a solution to my problem, or a hint to where I should start??
Adding the MyMenu() function to the add-in, and setting "this.ContextMenu" in e.g. the c'tor of the add-in works, so I guess I could implement a method in the add-in, which the host may invoke, sending the hosts ContextMenu object as parameter, and then doing the actual "set" of the property from inside the add-in. But that feels like going 'round a problem that shouldn't exist?!

I might also add, I've tried the exact same scenario, without using MAF... Created a WPF UserControl, containing its standard grid, only holding a button. Then created a standard WPF Application, created an instance of the usercontrol from within app code-behind, set the ContextMenu prop of the UC, added the UC to the app grid, and everything worked just fine... Right clicking the button in the UC, gave me the context menu applied by the app...

Thanks in advance
//J