2
votes

We are using some ActiveX or windows form control which don't have the WPF equivalences, so naturally we use WindowsFormsHost to host these controls. We normally make an UserControl out of it with some general controls such as button to implement the common functionality. One piece of xaml code is something like this:

<WindowsFormsHost Name="windowsFormsHost1" >   
    <WindowsFormsHost.ContextMenu>
        <ContextMenu>                    
            <MenuItem Header="_Test1" />
        </ContextMenu>                    
    </WindowsFormsHost.ContextMenu>

    <AxOWC:AxPivotTable x:Name="pivotTable" />        
</WindowsFormsHost>        
....

The AxPivotTable is a OWC (office web component) control. In another UserControl, we add a ReportViewer inside the WindowsFormsHost. Note that normally the AxPivotTable or ReportViewer has its default context menu even without any ContextMenu item I add.

So far my customized ContextMenu doesn't get showed yet (still the default one shows). Thanks to this question, I figured out I still need to capture the the mouse down event in the code-behind and set

windowsFormsHost1.ContextMenu.IsOpen = True

to show the contextmenu (weird though).

Now my problem is, only this Test1 ContextMenu is here now. The default ContextMenu won't show any more. As I mentioned, what we want is to add the customized on top of those default context menus.

1
are you able to dive in your windowsFormsHost1 to get the ContextMenu object? so you could do _yourContextMenu_.Items.Add(newMenuItem) - WiiMaxx
@WiiMaxx ,that's actually the part I don't understand. In my event handler where I set I set IsOpen=True, I can see the ContextMenu actually only contains my customized items, not the default menu provided by the control themselves. So I don't know how I can get these default menus so I can't add them(since the control are not in WPF assemblies I don't even know whether I can just use Items.Add to add them without conversion). In another word, my customized CM will overwrite the default since they are on different levels, and only if I don't have any customized one the default will be popped. - tete

1 Answers

0
votes

this would be my working "Solution" for your example above i don't know if you need this also for Child Elements from your WindowsFormsHost. Hope this will help you

XAML

<Window x:Class="tete.MainWindow"
        xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <Grid>
        <av:WindowsFormsHost Name="myWFH">
            <wf:ComboBox Name="myCBox">
            <wf:ComboBox.ContextMenu>
                <wf:ContextMenu>
                    <wf:ContextMenu.MenuItems>
                        <wf:MenuItem Text="somet"/>
                    </wf:ContextMenu.MenuItems>
                </wf:ContextMenu>
            </wf:ComboBox.ContextMenu>
        </wf:ComboBox>

    </Grid>
</Window>

Code-Behind

using System.Windows;
using System.Windows.Controls;

namespace tete
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        var child = myWFH.Child as System.Windows.Forms.ComboBox;
        child.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("my new menuitem"));
        }
    }
}

EDIT i hope now it fit's your needs :) and i understand correctly what you want ToDo