从MVVM中的视图模型绑定命令到WPF菜单 [英] Binding command to WPF menu from view model in MVVM

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

问题描述

大家好,


我有一个命令对象,我必须将所有菜单项绑定到同一个命令对象。我使用命令参数来区分不同的菜单项。


如何将Command对象绑定到视图模型中动态生成的菜单项?



我尝试过:


1。我试图直接在ViewModel中设置Command Object菜单。


2。我尝试使用SetBinding来设置命令。



但这些解决方案都不起作用。




Veeresh Angadi

解决方案


它应该绑定如下所示。

< MenuItem.ItemContainerStyle> 
< Style TargetType =" {x:Type MenuItem}">
< Setter Property =" Command" Value =" {Binding DataContext.ImportRecentItemCommand,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type MenuItem},AncestorLevel = 1}}" />
< Setter Property =" CommandParameter"值= QUOT; {结合}" />
< / Style>
< /MenuItem.ItemContainerStyle>

WPF - 如何使用绑定创建菜单和子菜单


最好的问候,


鲍勃


Hi Guys,

I have one command object and i have to bind all menu items to same command object. I use the command parameter to differentiate between different menu items.

How can i bind the Command object to dynamically generated menu item in view model?

What i have tried:

1. I tried to set Command Object of menu directly in ViewModel.

2. I tried to use SetBinding to set the command.

But none of these solutions are working.


Veeresh Angadi

解决方案

Hi,

It should bind as below.

         <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                    <Setter Property="CommandParameter" Value="{Binding}" />
                </Style>
            </MenuItem.ItemContainerStyle>

WPF - How can I create menu and submenus using binding

Best Regards,

Bob


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

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