“No target found for method” thrown by Caliburn Message.Attach()

前端 未结 2 399
悲&欢浪女
悲&欢浪女 2020-12-09 22:03

I have a list box for which I am styling ItemContainer to include a context menu. Here is the xaml for the same.


    

        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 22:29

    Your problem lies in that you are trying to bind the target to an element which doesn't exist in the same visual tree e.g. you have a ContextMenu on which the item resides.

    To correctly get an action target, you need to use the ContextMenus PlacementTarget property.

    Check out the following answer on SO for the XAML

    WPF Context Menus in Caliburn Micro

    So the following XAML should work:

    
    

    This should look for the PlacementTarget on the ContextMenu and set the target for the action to the value of PlacementTarget.Tag (which should be the ListBoxItem).

    If you set ListBoxItem.Tag (as you have already done) to be the DataContext of the parent container (the ListBox) you should be ok

    so the tag binding should be:

    
    

    e.g. the whole thing should be:

    
        
    
    

提交回复
热议问题