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

编程入门 行业动态 更新时间:2024-10-26 07:31:16
本文介绍了我可以使用Caliburn绑定到RoutedCommands吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

What's the best way to use WPF's built-in RoutedCommands with Caliburn?

例如,在我的外壳中,我有一个编辑菜单,其中的复制项附加到在 ApplicationCommands 中找到的标准命令:

For example, in my shell I have an Edit menu with a Copy item in it attached to the standard command found in ApplicationCommands:

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

我希望此项目由 TextBox 何时具有焦点,并由我自己的控件何时具有焦点。在我的控件中,通过创建 CommandBinding

I want this item to be handled by a TextBox when it has the focus and by my own controls when they have the focus. In my controls I can handle Execute and CanExecute in code behind by creating a CommandBinding:

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

有没有办法使用Caliburn处理ViewModel中的方法,或者重定向到我从ViewModel公开的另一个命令?还是我走错路了?

Is there a way, using Caliburn, to handle with methods in my ViewModel instead, or to redirect to another command that I expose from the ViewModel? Or am I going about this the wrong way?

推荐答案

我最终创建了行为,该行为允许剪贴板的RoutedCommands重定向到其他命令。

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

更多推荐

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

本文发布于:2023-10-13 09:40:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   绑定   Caliburn   RoutedCommands

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!