3

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>  

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?

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

G2Mula
  • 184
  • 1
  • 1
  • 9
Anders
  • 17,306
  • 10
  • 76
  • 144

2 Answers2

8

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)"
Anders
  • 17,306
  • 10
  • 76
  • 144
  • Sorry, but where exactly do I add `cal:Message.Attach="ShowSettings($originalsourcecontext)"` ? Is it in the `` ? But if it is, then "Plugins" menu will be clickable, and I only want its children to be clickable. – JobaDiniz Jan 05 '16 at 13:53
  • 2
    Please have a look here https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Views/Main/MainMenuView.xaml#L62 – Anders Jan 05 '16 at 14:17
0

(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

Leo
  • 7,379
  • 6
  • 28
  • 44
  • MenuItem does not have that event.. I have done something similar, but the context is wrong, i have a thread going at codeplex http://caliburnmicro.codeplex.com/discussions/287228 – Anders Jan 26 '12 at 08:13