Caliburn.Micro子菜单项如何单击才能在包含视图的视图模型上调用动作? [英] How can a Caliburn.Micro sub menuitem click call an action on the containing view's viewmodel?

查看:91
本文介绍了Caliburn.Micro子菜单项如何单击才能在包含视图的视图模型上调用动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ShellView中有一个顶层菜单,选择子MenuItem时,我想在ShellViewModel(Conductor.Collection.AllActive)上调用以下方法.

I have a top level menu in my ShellView and when selecting a sub MenuItem, I would like to call the following method on the ShellViewModel (a Conductor.Collection.AllActive).

    public void SelectServer(string pServerName)
    {
        mDefaultClaimServer = pServerName;
    }

以下内容不起作用,因为未调用任何方法(我尝试过各种签名和操作参数)-

The following does not work as no method gets called (I have tried various signatures and action parameters) -

    <Menu Name="menu1" DockPanel.Dock="Top">
        <MenuItem Header="Select Server" Name="ClaimServers">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <!-- we need this else we show the class name -->
                    <TextBlock Text="{Binding DisplayName}">
                        <ContentControl cal:Message.Attach="[Event Click] = [Action TxTester.ShellViewModel.SelectServer($Text)]"/>
                    </TextBlock>
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
    </Menu>

以下内容确实调用了ShellViewModel SelectServer方法,但是单击的子MenuItem的文本为空(我也尝试了许多其他签名和操作参数)-

The following does call the ShellViewModel SelectServer method but I get null for the text of the clicked sub MenuItem (I also tried many other signatures and action parameters) -

    <Menu Name="menu1" DockPanel.Dock="Top">
        <MenuItem Header="Select Server" Name="ClaimServers" cal:Message.Attach="SelectServer($this.Text)">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <!-- we need this else we show the class name -->
                    <TextBlock Text="{Binding DisplayName}" />
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
    </Menu>

我已经为此苦苦挣扎很长时间了,无法解决.有人可以建议适当的组合,使我可以将子MenuItem的标题文本传递给ShellViewModel SelectServer方法吗?

I've been struggling with this a long time and can't figure it out. Can someone suggest the proper combination where I can pass the header text of a sub MenuItem to the ShellViewModel SelectServer method?

推荐答案

罗伯·艾森伯格(Rob Eisenberg)在一篇帖子中描述了从绑定子菜单中获取文本的特殊技巧",我得到了我要尝试的工作- href ="http://caliburnmicro.codeplex.com/discussions/287228" rel ="noreferrer"> http://caliburnmicro.codeplex.com/discussions/287228

I got what I was trying to do working, per a post from Rob Eisenberg describing a "special trick to get the text from bound submenus" here - http://caliburnmicro.codeplex.com/discussions/287228

如果有人有建议,我仍然很想知道如何使用标准的OOTB逻辑来做我想做的事情,以便我能够更好地理解CM.

I would still love to know how to do what I was trying to do with standard OOTB logic if anyone has suggestions, so that I am able to understand CM better.

基本上,我将其添加到了引导程序的Configure()覆盖-

Basically I added this to the bootstrapper Configure() overide -

        MessageBinder.SpecialValues.Add("$originalsourcecontext", context =>
        {
            var args = context.EventArgs as RoutedEventArgs;
            if (args == null)
                return null;

            var fe = args.OriginalSource as FrameworkElement;
            if (fe == null)
                return null;

            return fe.DataContext;
        });

并将其添加到xaml-

and added this to the xaml -

        <MenuItem Header="_Select Server" Name="ClaimServers" cal:Message.Attach="SelectServer($originalsourcecontext)" />

然后我得到了我想要的子菜单项的标题文本.

and then I was passed the header text of the sub menuitem which is what I wanted.

这篇关于Caliburn.Micro子菜单项如何单击才能在包含视图的视图模型上调用动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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