ContextMenu with Command binding not working.

Apr 18, 2013 at 12:08 PM
Edited Apr 18, 2013 at 12:10 PM
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...
Apr 18, 2013 at 1:31 PM
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
Apr 28, 2013 at 3:57 PM
Edited Apr 28, 2013 at 8:33 PM
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...
Apr 29, 2013 at 12:33 PM
Can you try replacing the oxy:Plot control by a Canvas? Does the context menu command binding work in that case?
May 2, 2013 at 10:21 AM
Edited May 2, 2013 at 10:21 AM
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