1

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.

ThEpRoGrAmMiNgNoOb
  • 1,256
  • 3
  • 23
  • 46
  • Can you share your `TransactionViewModel`? I guess, that problem can be in relative source binding `RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}` – Pavel Anikhouski Nov 11 '19 at 09:24
  • @PavelAnikhouski my viewmodel is simply a constructor and the method RemoveClicked(). It doesn't really have a connection with the menuitem except for the RemoveClicked method. – ThEpRoGrAmMiNgNoOb Nov 11 '19 at 09:47

2 Answers2

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

I'm not sure, but you can test this:

<MenuItem Header="Remove Group" cal:Message.Attach="RemoveClicked" />
Mahdi Asgari
  • 272
  • 1
  • 13