使用MVVM,怎么能一个文本菜单视图模型发现,打开文本菜单视图模型? [英] Using MVVM, how can a ContextMenu ViewModel find the ViewModel that opened the ContextMenu?

查看:141
本文介绍了使用MVVM,怎么能一个文本菜单视图模型发现,打开文本菜单视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVVM绑定视图对象树。我有一个实现在我的树项目的基类,和基类有一个ContextMenu属性:

I'm using MVVM to bind views to objects in a Tree. I have a base class that implements the items in my tree, and that base class has a ContextMenu property:

    public IEnumerable<IMenuItem> ContextMenu
    {
        get
        {
            return m_ContextMenu;
        }
        protected set
        {
            if (m_ContextMenu != value)
            {
                m_ContextMenu = value;
                NotifyPropertyChanged(m_ContextMenuArgs);
            }
        }
    }
    private IEnumerable<IMenuItem> m_ContextMenu = null;
    static readonly PropertyChangedEventArgs m_ContextMenuArgs =
        NotifyPropertyChangedHelper.CreateArgs<AbstractSolutionItem>(o => o.ContextMenu);



结合至该基类(和所有派生类)观实现了一个文本菜单结合到该属性:

The View that binds to the base class (and all derived classes) implements a ContextMenu that binds to that property:

<ContextMenu x:Name="contextMenu" ItemsSource="{Binding Path=(local:AbstractSolutionItem.ContextMenu)}"
             IsEnabled="{Binding Path=(local:AbstractSolutionItem.ContextMenuEnabled)}"
             ItemContainerStyle="{StaticResource contextMenuStyle}"/>

在菜单中每个项目都绑定到的 IMenuItem 对象(视图模型的菜单项)。当你点击该菜单项,就用命令的基础对象执行命令。这一切的伟大工程。

Each item in the menu is bound to an IMenuItem object (a ViewModel for the menu items). When you click on the menu item, it uses Commands to execute a command on the base object. This all works great.

然而,一旦命令在IMenuItem类执行,有时需要去的对象的引用,用户右点击带来上下文菜单(或对象的视图模型,至少)。这是一个的背景的菜单的整点。我应该如何去传递树项目视图模型的参考菜单项视图模型?请注意,某些上下文菜单通过在树许多对象共享。

However, once the command is executing on the IMenuItem class, it sometimes needs to get a reference to the object that the user right clicked on to bring up the context menu (or the ViewModel of that object, at least). That's the whole point of a context menu. How should I go about passing the reference of the tree item ViewModel to the MenuItem ViewModel? Note that some context menus are shared by many objects in the tree.

推荐答案

我解决了这个由父处理的ContextMenuOpening事件控制(即拥有在查看文本菜单的)。我还添加了上下文属性IMenuItem。该处理程序是这样的:

I solved this by handling the ContextMenuOpening event on the parent control (the one that owned the ContextMenu in the View). I also added a Context property to IMenuItem. The handler looks like this:

    private void stackPanel_ContextMenuOpening(
        object sender, ContextMenuEventArgs e)
    {
        StackPanel sp = sender as StackPanel;
        if (sp != null)
        {
            // solutionItem is the "context"
            ISolutionItem solutionItem =
                sp.DataContext as ISolutionItem;
            if (solutionItem != null) 
            {
                IEnumerable<IMenuItem> items = 
                    solutionItem.ContextMenu as IEnumerable<IMenuItem>;
                if (items != null)
                {
                    foreach (IMenuItem item in items)
                    {
                        // will automatically set all 
                        // child menu items' context as well
                        item.Context = solutionItem;
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
        }
        else
        {
            e.Handled = true;
        }
    }

这需要一个事实,即只能有优势1文本菜单同时打开。

This takes advantage of the fact that there can only be one ContextMenu open at a time.

这篇关于使用MVVM,怎么能一个文本菜单视图模型发现,打开文本菜单视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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