MahApps 静态 DropDownButton 和 Caliburn.Micro 事件绑定

MahApps static DropDownButton and Caliburn.Micro event binding

我正在尝试使用 Caliburn.Micro 将打印事件附加到我的 MahApp DropDownButton 控件:

<metro:DropDownButton x:Name="PrintMenu" ToolTip="{brix:Loc PrintTT}" ButtonStyle="{DynamicResource MetroCircleButtonStyle}" FocusVisualStyle="{DynamicResource MetroCircleButtonFocusVisual}" Background="Transparent" ArrowVisibility="Collapsed" BorderBrush="Transparent">
    <metro:DropDownButton.Icon>
        <Rectangle Width="20" Height="20" Fill="{DynamicResource BackgroundBrush2}" Style="{StaticResource AppbarButtons}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_printer}" />
            </Rectangle.OpacityMask>
        </Rectangle>
    </metro:DropDownButton.Icon>
    <metro:DropDownButton.ItemsSource>
        <x:Array Type="StackPanel">
            <StackPanel>
                <Button cal:Message.Attach="Print" cal:Action.TargetWithoutContext="{Binding Path=DataContext, ElementName=PrintMenu}">
                    <StackPanel Orientation="Horizontal">
                        <Label>Print label</Label>
                        <Label Margin="10 0 0 0">CTRL+P</Label>
                    </StackPanel>
                </Button>
            </StackPanel>
        </x:Array>
    </metro:DropDownButton.ItemsSource>
</metro:DropDownButton>

然而,这给了我以下错误:

An unhandled exception of type 'System.Exception' occurred in WindowsBase.dll
Additional information: No target found for method Print.

关于我应该看什么有什么想法吗?我已经尝试添加 `cal:Action.TargetWithoutContext 以期解决问题,但这并没有帮助。

谢谢。

我通过在视图模型中构建打印命令列表来管理一种不同的方法。我的 DropDownButton 标记现在看起来像这样:

<metro:DropDownButton x:Name="PrintMenu" ToolTip="{brix:Loc PrintTT}" ButtonStyle="{DynamicResource MetroCircleButtonStyle}" FocusVisualStyle="{DynamicResource MetroCircleButtonFocusVisual}" Background="Transparent" ArrowVisibility="Collapsed" BorderBrush="Transparent" ItemsSource="{Binding PrintMenuCommands}">
    <metro:DropDownButton.Icon>
        <Rectangle Width="20" Height="20" Fill="{DynamicResource BackgroundBrush2}" Style="{StaticResource AppbarButtons}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_printer}" />
            </Rectangle.OpacityMask>
        </Rectangle>
    </metro:DropDownButton.Icon>
    <metro:DropDownButton.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding CommandText}" />
            <Setter Property="InputGestureText" Value="{Binding CommandShortcut}" />
            <Setter Property="Command" Value="{Binding Command}" />
        </Style>
    </metro:DropDownButton.ItemContainerStyle>
</metro:DropDownButton>

而我的 PrintMenuCommands 是 PrintMenuCommand 对象的列表:

public List<PrintMenuCommand> PrintMenuCommands { get; private set; } = new List<PrintMenuCommand>();

public struct PrintMenuCommand
{
    public string CommandText { get; set; }

    public string CommandShortcut { get; set; }

    public ICommand Command { get; set; }
}

private void AddPrintOptions()
{
    PrintMenuCommands.Add(new PrintMenuCommand
    {
        CommandText = "Print",
        CommandShortcut = "CTRL+P",
        Command = new PrintCommand()
    });
}