代码之家  ›  专栏  ›  技术社区  ›  GraemeF

我可以使用Caliburn绑定到RoutedCommands吗?

  •  3
  • GraemeF  · 技术社区  · 14 年前

    使用WPF的内置RoutedCommands的最佳方法是什么 Caliburn

    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 回复  |  直到 14 年前
        1
  •  1
  •   Adam Plocher    7 年前

    我最终创造了 a behaviour

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