ContextMenu with Command binding not working.

Oystein Bjorke 6 years ago 0
This discussion was imported from CodePlex

tboloo wrote at 2013-04-18 13:08:

Dear oxyplotters,
I am trying to add the ContextMenu to the plot, and bind some command to it. HEre is my code:
<oxy:Plot Model="{Binding Plot.Model}">
                        <MenuItem Command="{Binding Plot.ShotAtCommand}" Header="Shot At"/>
                        <MenuItem Command="{Binding Plot.AddShotCommand}" Header="Add Shot"/>
however the command are not executing, and (what is more surprising
) I do not get any binding errors, regardless whether I bind to existing or non existing command in the ViewModel. Any help would be appreciated...

objo wrote at 2013-04-18 14:31:

The right mouse button click is handled by OxyPlot, this is probably why the context menu is not showing up (and executing the bindings).
I think this is related to issue

tboloo wrote at 2013-04-28 16:57:

Thank you very much for the answer, however my situation is slightly different - the context menu is shown, only the bindings to the menu item commands are not executed, and any binding errors (i.e. binding to nonexisting commands) are not reported.
Still looking for a way to execute context menu commands...

objo wrote at 2013-04-29 13:33:

Can you try replacing the oxy:Plot control by a Canvas? Does the context menu command binding work in that case?

tboloo wrote at 2013-05-02 11:21:

I managed to get the bindings working. It turned out that ContextMenu DataContext was null, so I implemented a workaround found on StackOverflow which sets the ContextMenu DataContext in Loaded event, as follows:
        private void ContextMenu_Loaded(object sender, System.Windows.RoutedEventArgs e)
            (sender as ContextMenu).DataContext = this.DataContext;
I don't know if this is the best solution, but it is working :D