Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

wpf - Dynamic menus with Caliburn micro

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...