Dynamic menus with Caliburn micro

余生长醉 提交于 2019-12-04 13:49:14

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

You can then use it from xaml like this

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

(sorry for my bad english)

You can call a especific method on your VM using the syntax (on your XAML):

cal:Message.Attach="[Event SelectionChanged] = [Action ItemClick($this)]"

This will call a ItemClick method on the VM passing the bounded item itself as parameter. If this is a "PluginItem" with an execute method (like normally is), inside that method you just need to call it:

    public void ItemClick(PluginItem item)
    {
        item.Execute();
    }

You can read more about Actions here: http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=Documentation

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