MVVM 绑定命令到上下文菜单项 [英] MVVM binding command to contextmenu item

查看:22
本文介绍了MVVM 绑定命令到上下文菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将命令绑定到 WPF 中的菜单项.我使用的方法与我所有其他命令绑定的方法相同,但我不明白为什么它在这里不起作用.

I'm trying to bind a command to a menuitem in WPF. I'm using the same method that's been working for all my other command bindings, but I can't figure out why it doesn't work here.

我目前正在像这样绑定我的命令:

I'm currently binding my commands like this:

Command = "{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MyCommand}"

这是出错的地方(这是在 UserControl 内)

This is where it goes wrong (this is inside a UserControl)

<Button Height="40" Margin="0,2,0,0" CommandParameter="{Binding Name}" 
                        Command = "{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ConnectCommand}">

     <Button.ContextMenu>
         <ContextMenu>
             <MenuItem Header="Remove" CommandParameter="{Binding Name}"
                                      Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.RemoveCommand}"/>
         </ContextMenu>
     </Button.ContextMenu>
     ...

第一个命令绑定像它应该的那样工作,但第二个拒绝做任何事情.我尝试更改祖先级别并命名我的控件以通过 ElementName 而不是 RelativeSource 来访问它,但仍然没有变化.它一直说找不到与引用绑定的源..."

The first command binding works like it should, but the second one refuses to do anything. I've tried changing the ancestor level and naming my Control to access it through ElementName instead of RelativeSource, but still no change. It keeps saying "Cannot find source for binding with reference..."

我错过了什么?

推荐答案

(Edit) 既然你提到这是在 ItemsControl 的模板中,事情就不一样了:

(Edit) Since you mentioned this is in an ItemsControl's template, things are different:

1) 从这个博客获取 BindingProxy 类(并阅读博客,因为这是有趣的信息):DataContext未继承时如何绑定数据不是继承的.

1) Get the BindingProxy class from this blog (and read the blog, as this is interesting information): How to bind to data when the DataContext is not inherited.

ItemsControl(或 ContextMenu)中的元素基本上不是可视化或逻辑树的一部分,因此无法找到您的 UserControl 的 DataContext.我很抱歉没有在这里写更多,但作者已经很好地一步一步地解释了它,所以我无法在几行中给出完整的解释.

Basically the elements in the ItemsControl (or ContextMenu) are not part of the visual or logical tree, and therefore cannot find the DataContext of your UserControl. My apologies for not writing more on this here, but the author has done a good job explaining it step by step, so there's no way I could give a complete explanation in just a few lines.

2) 执行以下操作:(您可能需要对其进行一些调整以使其在您的控制范围内工作):

2) Do something like this: (you may have to adapt it a bit to make it work in your control):

一个.这将使您可以使用 StaticResource 访问 UserControl DataContext:

a. This will give you access to the UserControl DataContext using a StaticResource:

<UserControl.Resources>
<BindingProxy
  x:Key="DataContextProxy"
  Data="{Binding}" />
</UserControl.Resources>

B.这使用了 (a) 中定义的 DataContextProxy:

b. This uses the DataContextProxy defined in (a):

<Button.ContextMenu>
 <ContextMenu>
     <MenuItem Header="Remove" CommandParameter="{Binding Name}"
         Command="{Binding Path=Data.RemoveCommand, Source={StaticResource DataContextProxy}}"/>
 </ContextMenu>

这在树和数据网格等方面对我们有用.

This has worked for us in things like trees and datagrids.

这篇关于MVVM 绑定命令到上下文菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆