本文介绍了MVVM问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在使用MVVM在WPF中进行项目.主要思想是,我可以通过一些按钮元素(例如工具箱)来添加设计图面.我想在每个这样的项目上都有一个包含一些命令的上下文菜单.我有添加的项目的ViewModel(项目已将其DataContext属性设置为该项目),我想将其作为命令参数传递给菜单项命令,但由于我不知道如何添加它,我被卡住了.我正在使用以下xaml:

Hello,

I am working on a project in WPF using MVVM. The main idea is that I have a design surface on wich I can add via some buttons element (like a toolbox). I want to have on each such item, a context menu with some commands. I have the ViewModel of the item I add (item has its DataContext property set to it) and I want to pass it as command parameter to menu items commands and I''m stuck because I don''t know how to acces it. I''m using the folowing xaml :

<Style x:Key="BlockContainerStyle" TargetType="{x:Type ContentControl}" >
   ....
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu ItemsSource="{Binding Path=MenuItems}" ItemContainerStyle="{StaticResource ContextMenuItemStyle}"/>
            </Setter.Value>
        </Setter>
    </Style>


....


....

<Style x:Key="ContextMenuItemStyle">
        <Setter Property="MenuItem.Header" Value="{Binding DisplayText}"/>
        <Setter Property="MenuItem.ItemsSource" Value="{Binding ChildItems}"/>
        <Setter Property="MenuItem.Command" Value="{Binding Command}" />
        <Setter Property="MenuItem.CommandParameter" Value="{Binding}"></Setter>
    </Style>



..并且我想将MenuItem.CommandParameter绑定到控件的DataContext,直到我应用BlockContainerStyle.我该怎么办?

谢谢



..and I want to bind the MenuItem.CommandParameter to the DataContext of the control on wich I apply the BlockContainerStyle. How should I do that ?

Thank you

推荐答案


这篇关于MVVM问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:12