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}">
                <oxy:Plot.ContextMenu>
                    <ContextMenu>
                        <MenuItem Command="{Binding Plot.ShotAtCommand}" Header="Shot At"/>
                        <MenuItem Command="{Binding Plot.AddShotCommand}" Header="Add Shot"/>
                    </ContextMenu>
                </oxy:Plot.ContextMenu>
     </oxy:Plot>
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...
Regards,
Bolek.
Coordinator
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 https://oxyplot.codeplex.com/workitem/9625
Apr 28, 2013 at 3:57 PM
Edited Apr 28, 2013 at 8:33 PM
objo,
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...
Regards,
Bolek.
Coordinator
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
objo,
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
Regards,
Bolek.