简体   繁体   English

WPF绑定到ContextMenu的命令

[英]WPF binding command to ContextMenu

I have a problem with command binding in WPF. 我在WPF中的命令绑定有问题。 I have the following xaml: 我有以下xaml:

<ItemsControl ItemsSource="{Binding Entity}" Name="Lst">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>                    
                <Button  Content="qwerty" Command="{Binding ElementName=Lst, Path=DataContext.SaveCommand}" >
                    <Button.ContextMenu>
                        <ContextMenu>                                
                            <MenuItem Header="Send2" Command="{Binding ElementName=Lst, Path=DataContext.SaveCommand}" />
                        </ContextMenu>
                    </Button.ContextMenu>    
                </Button>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

As you can see Button and its ContextMenu have the similar command-bindings. 如您所见,Button及其ContextMenu具有相似的命令绑定。 But when i click button its command is firing and when i click context menu's item its command isn't firing. 但是当我单击按钮时,它的命令被触发,而当我单击上下文菜单项时,它的命令不被触发。 Where am i wrong? 我哪里错了? Thanks in advance! 提前致谢!

I had a similar problem before and solved it by passing the datacontext through the tag property of the container as below. 我之前也遇到过类似的问题,并通过将datacontext通过容器的tag属性传递来解决它,如下所示。 I have it working on a grid ContextMenu but dont see any reason why this wont work on a button. 我有它在网格ContextMenu上工作,但看不到任何不能在按钮上工作的原因。 Let me know if you have any problem 如果您有任何问题,请告诉我

<Button  Content="qwerty" Tag="{Binding DataContext,ElementName=Lst}" Command="{Binding ElementName=Lst, Path=DataContext.SaveCommand}"  >
                    <Button.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">                                
                            <MenuItem Header="Send2" Command="{Binding SaveCommand}" />
                        </ContextMenu>
                    </Button.ContextMenu>    
                </Button>

The ContextMenu being separate from the visual tree, you cannot bind with and element outside of it. ContextMenu与视觉树是分开的,您不能与其绑定,也不能在其外部绑定元素。

If you check your output window, you should have a message saying that it can't find the object "Lst" 如果检查输出窗口,则应该显示一条消息,指出找不到对象“ Lst”

A common and easy workaround would be to manually set the DataContext in code-behind (note: this is not breaking MVVM at all. You are just performing a pure UI operation of linking DataContexts together): 一个常见且简单的解决方法是在代码隐藏中手动设置DataContext(注意:这根本不会破坏MVVM。您只是在执行将DataContext链接在一起的纯UI操作):

In your Xaml: 在您的Xaml中:

<Button.ContextMenu>
                        <ContextMenu Opened="OnContextMenuOpened">                                
                            <MenuItem Header="Send2" Command="{Binding ElementName=Lst, Path=DataContext.SaveCommand}" />
                        </ContextMenu>
                    </Button.ContextMenu>

In code-behind: 在后面的代码中:

public void OnContextMenuOpened(object sender, RoutedEventArgs args)
{
    (sender as ContextMenu).DataContext = Lst.DataContext;
}

You are therefore linking the DataContext every time the ContextMenu is opened (so if Lst's DataContext changes, your ContextMenu will as well) 因此,每次打开ContextMenu时,您都在链接DataContext (因此,如果Lst的DataContext发生更改,您的ContextMenu也将随之链接)

Alternatively (cleaner if you are bound to use it a lot of times), get the BindingProxy from this article: http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ and it'll do the trick! 或者,(如果更愿意多次使用它, BindingProxy ), BindingProxy从本文获取BindingProxyhttp : BindingProxy -当不继承数据上下文时 ,它会解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM