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

查看:197
本文介绍了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..."

我想念什么?

推荐答案

(编辑)由于您提到的是在ItemsControl的模板中,所以情况有所不同:

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

1)从此博客获取BindingProxy类(并阅读该博客,因为这是有趣的信息):

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):

a.这将使您可以使用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天全站免登陆