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

问题描述

我将Caliburn micro用于WPF项目.静态菜单易于与Caliburn绑定

I use Caliburn micro for my WPF Project. Static menus are easy to bind with Caliburn

<Menu Grid.Row="0" IsMainMenu="True">
    <MenuItem Header="_File">
        <MenuItem x:Name="OpenScript" Header="_Open script"/>
    </MenuItem>
    <MenuItem Header="_Script">
        <MenuItem x:Name="RunScript" Header="_Run script" />
        <MenuItem x:Name="StopScript" Header="_Stop script" />
    </MenuItem>
    <MenuItem Header="S_ettings">
        <MenuItem x:Name="Plugins" Header="_Plugins">...Clickable children here</MenuItem>
    </MenuItem>
</Menu>

名称绑定到模型上的方法,但是对于您在上方看到的Plugins菜单,我们需要绑定到PluginViewModel的集合.然后,当您单击插件时,我希望Caliburn操作方法在菜单上触发视图模型(您现在可以从中产生reuturn IResults的类型).这可能吗?

The names are bound to methods on the model, but for the Plugins menu that you see above we need to bind against a collection of PluginViewModel.. Then when you click a plugin i want a Caliburn action method to trigger on the menu view model (You now the kind that you can yield reuturn IResults from).. Is this possible?

这个问题是针对这个开源项目的 https://github.com/AndersMalmgren/FreePIE

This question is for this open source projecthttps://github.com/AndersMalmgren/FreePIE

忘了提到我已经解决了绑定部分,

edit: Forgot to mentioned that i have solved the binding part,

public BindableCollection<PluginMenuViewModel> Plugins { get; set; }

但是我不知道如何聆听模型的点击

But i do not know how to listen to the click from the model

推荐答案

最好的方法是添加自己的消息绑定器

The best way is to add your own message binder

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使用它

You can then use it from xaml like this

cal:Message.Attach="ShowSettings($originalsourcecontext)"

这篇关于Caliburn micro的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:58