Search code examples
c#wpfmvvmcaliburn.microcaliburn

"No target method found" error using cal:Message.Attach in WPF Context Menu


I am using Caliburn.Micro. I have tried the solutions I found for this problem but is no good. I have the following XAML code for my design:


<Grid x:Name="ActionGrid">
    <MenuItem Header="Action" FontFamily="Open Sans" FontSize="14" HorizontalContentAlignment="Right" Foreground="White" x:Name="miAction" Margin="5" Background="#FF166FC4" Tag="{Binding DataContext}">
         <MenuItem.Style>
               <Style TargetType="{x:Type MenuItem}">
                      <Style.Triggers>
                          <EventTrigger RoutedEvent="Click">
                                 <EventTrigger.Actions>
                                        <BeginStoryboard>
                                              <Storyboard>
                                                   <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                                          <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                                   </BooleanAnimationUsingKeyFrames>
                                              </Storyboard>
                                        </BeginStoryboard>
                                 </EventTrigger.Actions>
                          </EventTrigger>
                      </Style.Triggers>
                      <Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}}"/>
                      <Setter Property="ContextMenu">
                            <Setter.Value>
                                    <ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}">
                                           <MenuItem Header="Remove Group" cal:Message.Attach="RemoveClicked()" />
                                    </ContextMenu>
                            </Setter.Value>
                      </Setter>
             </Style>
        </MenuItem.Style>
   </MenuItem>
</Grid>

<UserControl.DataContext>
        <vm:TransactionViewModel/>
</UserControl.DataContext>

Everytime I click on the Item, it returns No Method Found for RemoveClicked. I don't know what I did wrong. Please help me point it out.


Solution

  • Tag="{Binding DataContext}" should be Tag="{Binding}" and the cal:Action.TargetWithoutContext attached property should be set on the MenuItem. Then it works if you right click on the MenuItem to open the ContextMenu:

    <MenuItem Header="Action" FontFamily="Open Sans" FontSize="14" HorizontalContentAlignment="Right" Foreground="White" x:Name="miAction" 
                      Margin="5" Background="#FF166FC4" Tag="{Binding}">
        <MenuItem.Style>
            <Style TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                    </BooleanAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu>
                            <MenuItem Header="Remove Group"
                                              cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"
                                              cal:Message.Attach="RemoveClicked()" />
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </MenuItem.Style>
    </MenuItem>
    

    Use an EventTrigger to open the ContextMenu on left click doesn't work with bindings and this has nothing to do with Caliburn.Micro:

    WPF Context menu on left click

    You may replace the EventTrigger with an attached behaviour.