Can I use Caliburn to bind to RoutedCommands?

試著忘記壹切 提交于 2019-12-05 19:55:29
GraemeF

I ended up creating a behaviour that allows the clipboard RoutedCommands to be redirected to other commands.

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!