带有 Caliburn micro 的动态菜单 [英] Dynamic menus with Caliburn micro

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

问题描述

我的 WPF 项目使用 Caliburn micro.静态菜单很容易与 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>  

名称绑定到模型上的方法,但是对于您在上面看到的插件菜单,我们需要绑定到 PluginViewModel 的集合.然后当您单击插件时,我希望在菜单上触发 Caliburn 操作方法视图模型(您现在可以从中产生返回 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 project https://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 的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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