根据http://www.voidcn.com/article/p-rtutndyr-bvo.html这篇文章给出的解决方案,我将完整的解决代码总结下来
方案1:WPF 4.0或更高版本
使用x:Reference标记扩展来与ItemsControl dataContext绑定,在ItemsControl上设置x:Name并使用绑定:
<ItemsControl x:Name="itemsControl">
....
<MenuItem Command="{Binding DataContext.Command,
Source={x:Reference itemsControl}}"/>
....
</ItemControl>
方案2:WPF 低于4.0
使用Freezable BindingProxy方法:
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore() => new BindingProxy();
public object Data
{
get => GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
public static readonl