"No target method found" 使用 cal:Message 时出错。在 WPF 上下文菜单中附加

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

我正在使用 Caliburn.Micro。我已经尝试了针对此问题找到的解决方案,但效果不佳。我的设计有以下 XAML 代码:


<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>

每次我点击项目时,它 returns No Method Found for RemoveClicked。我不知道我做错了什么。请帮我指出来。

我不确定,但你可以测试一下:

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

Tag="{Binding DataContext}" 应该是 Tag="{Binding}" 并且 cal:Action.TargetWithoutContext 附加 属性 应该设置在 MenuItem 上。如果你右键点击MenuItem打开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>

使用 EventTrigger 左侧打开 ContextMenu 单击不适用于绑定,这与 Caliburn.Micro:

WPF Context menu on left click

您可以将 EventTrigger 替换为 an attached behaviour