2010-07-29 5 views
3

Quelle est la meilleure façon d'utiliser les commandes intégrées RoutedCommands de WPF avec Caliburn?Puis-je utiliser Caliburn pour lier à RoutedCommands?

Par exemple, dans ma coquille J'ai un menu Edition avec un élément de copie en elle associée à la commande standard trouvée dans les ApplicationCommands:

<Menu> 
    <MenuItem Header="Edit"> 
     <MenuItem Header="Copy" 
        Command="ApplicationCommands.Copy" /> 
    </MenuItem> 
</Menu> 

Je veux cet article à être manipulé par un TextBox quand il a la mise au point et par mes propres contrôles quand ils ont l'accent. Dans mes contrôles, je peux gérer Execute et CanExecute dans le code derrière en créant un CommandBinding:

<UserControl.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Copy" 
        Executed="CopyCommandExecute" 
        CanExecute="CanCopyCommandExecute" /> 
</UserControl.CommandBindings> 

Est-il possible, en utilisant Caliburn, à manipuler avec des méthodes dans mon lieu ViewModel ou rediriger vers une autre commande que j'exposerai à partir du ViewModel? Ou est-ce que je vais à ce sujet dans le mauvais sens?

Répondre

1

J'ai fini par créer a behaviour qui permet de rediriger les commandes routées du Presse-papiers vers d'autres commandes.

public class ClipboardBehavior : Behavior<Control> 
{ 
    public static readonly DependencyProperty CopyCommandProperty = 
     DependencyProperty.Register("CopyCommand", 
            typeof (ICommand), 
            typeof (ClipboardBehavior), 
            new PropertyMetadata(default(ICommand))); 

    public static readonly DependencyProperty CutCommandProperty = 
     DependencyProperty.Register("CutCommand", 
            typeof (ICommand), 
            typeof (ClipboardBehavior), 
            new PropertyMetadata(default(ICommand))); 

    public static readonly DependencyProperty DeleteCommandProperty = 
     DependencyProperty.Register("DeleteCommand", 
            typeof (ICommand), 
            typeof (ClipboardBehavior), 
            new PropertyMetadata(default(ICommand))); 

    public static readonly DependencyProperty PasteCommandProperty = 
     DependencyProperty.Register("PasteCommand", 
            typeof (ICommand), 
            typeof (ClipboardBehavior), 
            new PropertyMetadata(default(ICommand))); 

    public ICommand DeleteCommand 
    { 
     get { return (ICommand) GetValue(DeleteCommandProperty); } 
     set { SetValue(DeleteCommandProperty, value); } 
    } 

    public ICommand CutCommand 
    { 
     get { return (ICommand) GetValue(CutCommandProperty); } 
     set { SetValue(CutCommandProperty, value); } 
    } 

    public ICommand CopyCommand 
    { 
     get { return (ICommand) GetValue(CopyCommandProperty); } 
     set { SetValue(CopyCommandProperty, value); } 
    } 

    public ICommand PasteCommand 
    { 
     get { return (ICommand) GetValue(PasteCommandProperty); } 
     set { SetValue(PasteCommandProperty, value); } 
    } 

    protected override void OnAttached() 
    { 
     AddBinding(ApplicationCommands.Delete,() => DeleteCommand); 
     AddBinding(ApplicationCommands.Cut,() => CutCommand); 
     AddBinding(ApplicationCommands.Copy,() => CopyCommand); 
     AddBinding(ApplicationCommands.Paste,() => PasteCommand); 
    } 

    private void AddBinding(ICommand command, Func<ICommand> executingCommand) 
    { 
     var binding = new CommandBinding(command, 
             (sender, e) => Execute(e, executingCommand()), 
             (sender, e) => CanExecute(e, executingCommand())); 

     AssociatedObject.CommandBindings.Add(binding); 
    } 

    private static void CanExecute(CanExecuteRoutedEventArgs args, ICommand command) 
    { 
     if (command != null) 
     { 
      args.CanExecute = command.CanExecute(args.Parameter); 
      args.ContinueRouting = false; 
     } 
    } 

    private static void Execute(ExecutedRoutedEventArgs e, ICommand command) 
    { 
     if (command != null) 
      command.Execute(e.Parameter); 
    } 
}