2016-05-04 11 views
0

Ich versuche, einen Druckvorgang mit Caliburn.Micro meiner MahApp Dropdownbutton Kontrolle zu befestigen:MahApps statische und Dropdownbutton Caliburn.Micro Ereignisbindung

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

Das gibt mir die folgende Fehlermeldung jedoch:

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

Irgendwelche Ideen auf was ich schauen sollte? Ich habe versucht, das `cal: Action.TargetWithoutContext in der Hoffnung hinzuzufügen, das Problem zu beheben, aber das half nicht.

Danke.

+0

Wo Ihr Druck Veranstaltung kommen? – C1rdec

+0

Druckereignis ist eine Methode in meinem Ansichtsmodell. Es wird nur gut aufgerufen, wenn ich es dem DropDownButton zuweise, aber nicht, wenn es den einzelnen Buttons zugewiesen wird, die ItemsSource hinzugefügt werden. – DavidIQ

Antwort

0

Ich habe einen anderen Ansatz dazu durch Erstellen der Liste der Druckbefehle im Ansichtsmodell verwaltet. Mein Dropdownbutton Markup sieht nun wie folgt aus:

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

Und mein PrintMenuCommands ist eine Liste von PrintMenuCommand Objekte:

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() 
    }); 
}