How can a Caliburn.Micro sub menuitem click call an action on the containing view's viewmodel?

北城以北 提交于 2019-12-21 05:49:09

问题


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>

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>

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?


回答1:


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

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.

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;
        });

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.



来源:https://stackoverflow.com/questions/11279443/how-can-a-caliburn-micro-sub-menuitem-click-call-an-action-on-the-containing-vie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!