Button displaying a context menu

Topics: Actions & Coroutines
Sep 25, 2013 at 1:58 PM
Edited Sep 25, 2013 at 1:59 PM
I'm sorry as I know this has been asked already, but I can't find a solution that is working for my particular scenario.

I'm trying to simply display a ContextMenu when the user clicks on a button. I managed to display the items, but impossible to make the action trigger my ViewModel method, I receive a No target found for method from the ActionMessage.

Here's the code for my button.
<Button Name="Export2" Tag="{Binding DataContext, ElementName=LayoutRoot}" Content="Export">
        <Style TargetType="{x:Type Button}">
                <EventTrigger RoutedEvent="Click">
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
        <ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
            <MenuItem cal:Message.Attach="ExportToSvg" Header="SVG" />
            <MenuItem Name="ExportToPng2" Header="PNG" />
As you can see, I've tried setting the Action.TargetWithoutContext on my ContextMenu, but it doesn't seem to work.

My ViewModel has an ExportToSvg method defined like this:
public void ExportToSvg()
      throw new NotImplementedException();
What am I doing wrong?
Sep 26, 2013 at 7:30 PM
Have you tried checking if the Tag property correctly holds a reference to the view-model? And what about the PlacementTarget?
I would first check if everything is in place, then I would turn on CM logging feature to try to understand where it is searching the actual method.
Sep 26, 2013 at 7:52 PM
The Tag property has the correct reference to the view-model, but I can't find a way that binds it correctly to the TargetWithoutContext. The PlacementTarget the way I did it in my sample doesn't work.
Sep 26, 2013 at 8:58 PM
Edited Sep 26, 2013 at 8:58 PM
Uhmmm, is it a WPF project? I tipically bind directly to the PlacementTarget.DataContext property or to the TargetWithoutContext property, after it has been properly set.
Can you try using the the standard way to open the ContextMenu (right-click), to see if the way the ContextMenu is activated is getting in the way (even if I am not aware of something like this).

I just tested this, and it works (WPF)...
<Window x:Class="WpfApplication3.ShellView"
    <Grid Background="White">
        <Button VerticalAlignment="Top"
                Content="Click here!">
                <ContextMenu cm:Action.TargetWithoutContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.(cm:Action.TargetWithoutContext)}">
                    <MenuItem cm:Message.Attach="DoSomething"
                              Header="Do something!"/>
namespace WpfApplication3
    #region Namespaces
    using System.Windows;


    public class ShellViewModel : IShell
        public void DoSomething()
The project was created using a simple WPF project, with 2 NuGet packages (Caliburn.Micro and Caliburn.Micro.Start).
Sep 27, 2013 at 9:09 AM
OK, that's interesting!

When I open the ContextMenu on my button through a right-click it does work correctly. So it really is a problem with the EventTrigger...

Any idea on how I could fix this?