2010-07-29 47 views
3

Caliburn一起使用WPF的內置RoutedCommands的最佳方式是什麼?我可以使用Caliburn綁定到RoutedCommands嗎?

例如,在我的殼我有一個複製項目的編輯菜單在它附着在ApplicationCommands中的標準命令:

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

我想這個項目由TextBox進行處理時,它具有重點和我們自己的控制。在我的控制,我可以落後創建處理代碼ExecuteCanExecute一個CommandBinding

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

有沒有一種方法,利用卡利,在我的視圖模型方法來處理,而不是,或重定向到我揭露另一個命令從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); 
    } 
} 
相關問題