2010-07-29 5 views
3

Каков наилучший способ использования встроенных RouteCommands WPF с Caliburn?Могу ли я использовать Caliburn для привязки к RoutedCommands?

Например, в моей оболочке У меня есть меню Правки с пунктом Copy в нем, прикрепленный к стандартной команде, найденной в ApplicationCommands:

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

Я хочу этот пункт будет обрабатываться с помощью TextBox когда он имеет фокус и мои собственные элементы управления, когда у них есть фокус. В моем управлении я могу справиться с Execute и CanExecute в коде за счетом создания CommandBinding:

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

Есть ли способ, с помощью Caliburn, чтобы справиться с методами в моем ViewModel вместо этого, или перенаправить в другую команду, я разоблачить из ViewModel? Или я об этом ошибаюсь?

ответ

1

В итоге я создал a behaviour, что позволяет перенаправлять почтовые сообщения RoutedCommands другим командам.

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

 Смежные вопросы

  • Нет связанных вопросов^_^