Command binding not working in a dynamic MVVM Context Menu

狂风中的少年 提交于 2019-12-04 16:55:57

DataContext on ContextMenus can be weird, I bet if you look in the output window in Visual Studio when debugging that there will be a binding error for TheCommand not being found. Try the following:

<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext.TheCommand}"/> 

This will use the DataContext of the element that the ContextMenu is launched from, not the context menu itself.

Did you try

Value="{TemplateBinding TheCommand}" ?

dev27

Look at my answer for the following question -

Context Menu items command binding WPF using MVVM

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!