3

将 WPF 的内置 RoutedCommands 与Caliburn一起使用的最佳方式是什么?

例如,在我的 shell 中,我有一个 Edit 菜单,其中有一个 Copy 项,附加到以下标准命令ApplicationCommands

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

我希望这个项目在它有焦点时由 a 处理,TextBox当它们有焦点时由我自己的控件处理。在我的控件中,我可以Execute通过CanExecute创建一个CommandBinding

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

有没有办法使用 Caliburn 来处理我的 ViewModel 中的方法,或者重定向到我从 ViewModel 公开的另一个命令?还是我以错误的方式解决这个问题?

4

1 回答 1

1

我最终创建了一个允许剪贴板 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);
    }
}
于 2010-07-30T16:30:40.313 回答