WPF Context menu doesn't bind to right databound item

前端 未结 7 1317
青春惊慌失措
青春惊慌失措 2020-12-08 11:03

I have a problem when binding a command in a context menu on a usercontrol that is on a tab page. The first time I use the menu (right-click on the tab) it works great, but

7条回答
  •  感情败类
    2020-12-08 11:42

    I found this method using the Tag property very useful when binding from a context menu deep inside a control template:

    http://blog.jtango.net/binding-to-a-menuitem-in-a-wpf-context-menu

    This makes it possible to bind to any datacontext available to the control that the context menu was opened from. The context menu can access the clicked control through "PlacementTarget". If the Tag property of the clicked control is bound to a desired datacontext, binding to "PlacementTarget.Tag" from inside the context menu will slingshot you directly to that datacontext.

提交回复
热议问题