使用 Caliburn micro 的动态菜单

发布于 2024-12-28 16:54:53 字数 1066 浏览 6 评论 0原文

我在我的 WPF 项目中使用 Caliburn micro。静态菜单很容易与 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 的类型)..这可能吗?

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

编辑:忘记提及我已经解决了绑定部分,

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2025-01-04 16:54:54

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

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

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

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)"
删除会话 2025-01-04 16:54:54

(抱歉我的英语不好)

您可以使用语法(在 XAML 上)在 VM 上调用特定方法:

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

这将调用 VM 上的 ItemClick 方法,并将有界项本身作为参数传递。
如果这是一个具有执行方法的“PluginItem”(就像通常一样),则在该方法中您只需调用它:

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

您可以在此处阅读有关操作的更多信息:http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=文档

(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文