我正在尝试使用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以期解决问题,但这没有帮助。
感谢。
答案 0 :(得分:0)
我通过在视图模型中构建打印命令列表来管理不同的方法。我的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()
});
}