C# Caliburn Micro的动态菜单和子菜单,如何绑定命令?

C# Caliburn Micro的动态菜单和子菜单,如何绑定命令?,c#,wpf,caliburn.micro,commandbinding,C#,Wpf,Caliburn.micro,Commandbinding,我在一个WPF项目中使用Caliburn Micro,我希望插件组件能够填充工具栏。每个插件都有一个顶级菜单项,如果他们愿意,可以用子菜单填充它 <ToolBar> <Menu x:Name="ToolBarMenuItems"> <Menu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <S

我在一个WPF项目中使用Caliburn Micro,我希望插件组件能够填充工具栏。每个插件都有一个顶级菜单项,如果他们愿意,可以用子菜单填充它

<ToolBar>
    <Menu x:Name="ToolBarMenuItems">
        <Menu.ItemContainerStyle>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="Header" Value="{Binding Path=Text}" />
                <Setter Property="ItemsSource" Value="{Binding Path=Children}" />
            </Style>
        </Menu.ItemContainerStyle>
    </Menu>
</ToolBar>


但正如我所说,我需要对整个菜单执行此操作,而不仅仅是在菜单项上,但我不知道如何使用Caliburn Micro绑定这些方法。

向您的
样式添加一个setter,设置
cal:Message.Attach
附加属性并传递
$executionContext

<Menu x:Name="ToolBarMenuItems">
    <Menu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Header" Value="{Binding Path=Text}" />
            <Setter Property="ItemsSource" Value="{Binding Path=Children}" />
            <Setter Property="cal:Message.Attach" Value="RunCommand($executionContext)" />
        </Style>
    </Menu.ItemContainerStyle>
</Menu>

有关这方面的更多信息,请参阅问题。

我不了解您的问题,您能详细说明吗?您有一个menuitem的tips函数,但绑定方法的问题在哪里?显示您想要的示例..对不起,我的英语很抱歉,我的工作示例主要是一个菜单项,其中列表中的每个对象都是一个子菜单项。如果我有标准的菜单,如文件、编辑、插件等。这将是完美的,我会把它放在插件菜单项上,每个插件都会有一个子菜单来玩。但我不希望它出现在菜单项上,我希望它出现在菜单上,集合中的每个对象都应该是该菜单上的顶级菜单项。这是因为我想要一个工具栏,其中每个插件都有一个顶级菜单项,如果需要,可以在其中添加子菜单。我添加了一个图像来澄清。它工作得非常好,非常感谢!我没有意识到cal:Message.Attatch可以用作样式的属性,即使现在我看到它时它看起来很明显(通常是这样)。
<Menu>
    <MenuItem x:Name="ToolBarMenuItems" DisplayMemberPath="Text" Header="MyMenu" cal:Message.Attach="RunToolbarCommand($originalsourcecontext)"/>
</Menu>
MessageBinder.SpecialValues.Add("$originalsourcecontext", context =>
{
    var args = context.EventArgs as RoutedEventArgs;
    var fe = args?.OriginalSource as FrameworkElement;
    return fe?.DataContext;
});
<Menu x:Name="ToolBarMenuItems">
    <Menu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Header" Value="{Binding Path=Text}" />
            <Setter Property="ItemsSource" Value="{Binding Path=Children}" />
            <Setter Property="cal:Message.Attach" Value="RunCommand($executionContext)" />
        </Style>
    </Menu.ItemContainerStyle>
</Menu>
public void RunCommand(ActionExecutionContext context)
{
    if (context?.EventArgs is RoutedEventArgs routedEventArgs)
        routedEventArgs.Handled = true;

    MessageBox.Show("Run!");
}