上下文菜单中的绑定

问题描述:

<ContextMenu x:Key="ContextMenu">
  <MenuItem x:Name="MenuItemX" Header="2"/>
  <MenuItem Header="{Binding Header, ElementName=MenuItemX}"/>
</ContextMenu>



在上下文菜单内的绑定上使用ElementName似乎无效.
此链接 [



Using ElementName on a binding inside a context menu doesn''t seem to work.
This link[^] describes one way to solve it, however my context menu has no name since it is in a resource dictionary. Any other suggestions?

我倾向于使用 ^ ].


这是一个不错的解决方案,但是对于我眼前的问题却不是.如果我正在使用的代码是在MVVM中开发的,那么我可以将代码移至ViewModels,但是现在我被困在XAML领域,而代码却在后面.

正如我所看到的,这是Microsoft的一个错误,即您作为开发人员无法绑定到上下文菜单内的其他UI元素.我要寻找的是一种解决方法.
It is a nice solution, however not to my problem at hand. If the code I was working on was developed in MVVM, I could have moved the code to the ViewModels, however for now I am stuck in the land of XAML and code behind.

As I see it, it is a bug by Microsoft that you as a developer can''t bind to other UI elements inside a context menu. What I am searching for is a work around.