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.

2
  • Can you share your TransactionViewModel? I guess, that problem can be in relative source binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}} Commented Nov 11, 2019 at 9: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. Commented Nov 11, 2019 at 9:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

<MenuItem Header="Remove Group" cal:Message.Attach="RemoveClicked" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.