3

CaliburnでWPFの組み込みRoutedCommandsを使用するための最良の方法は何ですか?

たとえば、私のシェルには、次の場所にある標準コマンドに添付されたコピー項目を含む編集メニューがありますApplicationCommands

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

このアイテムは、フォーカスがある場合はaで処理されTextBox、フォーカスがある場合は自分のコントロールで処理されるようにします。私のコントロールでは、 :を作成することExecuteCanExecute、コードビハインドで処理できます。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 に答える