如何在 MVVM 中使用 ApplicationCommands [英] How to use ApplicationCommands in MVVM

查看:40
本文介绍了如何在 MVVM 中使用 ApplicationCommands的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ApplicationCommands.Cut、复制、粘贴、保存……它们看起来很有趣,因为命令路由、键绑定以及某些控件使用它们的事实.我了解如何绑定到 VM 上的中继/委托命令,但我似乎无法理解应用程序命令.我找到了几个旧答案,但没有其他信息,我有点不愿意遵循这些路线.

I would like to use ApplicationCommands.Cut, copy, paste, save,... They seem interesting because of the command routing, keybindings and the fact that some controls use them. I understand how I can bind to relay/delegatecommands on my VM but I can't seem to get my head around the Application commands. I found a couple of old answers but no other information and I am kind of reluctant of following those routes.

这似乎很常见,但信息似乎非常有限.这通常是如何实现的?(无论是否使用 PRISM、MVVM Light,...)

This seems like something common but yet informations seems to be very limited. How is this commonly achieved? (With or without using PRISM, MVVM Light, ...)

旧答案:

如何将 ApplicationCommands 绑定到 ViewModel 但这似乎很奇怪给我用一个行为来解决这个问题

How to bind ApplicationCommands to a ViewModel but this seems strange to me to solve this using a behavior

WPF - 在 ViewModel 中处理 ApplicationCommand 但我不在接受的答案中,这不是 MVVM.

WPF - Handle an ApplicationCommand in the ViewModel but I don't think that's MVVM in the accepted answer.

使用旧文章中的附加属性:CommandBindings with MVVM 引用另一篇文章.

Using attached properties in an old article: CommandBindings with MVVM referencing another article.

推荐答案

我问这个问题已经有一段时间了,但看起来很多人都在关注它.我最终使用了一种将 VM 命令列表连接到 FrameworkElement 的行为.这似乎是最灵活和可重用的解决方案.

It has been a while since I asked this question but it looks like a lot of people are looking at it. I ended up using a behavior which connects the VM command list to the FrameworkElement. This seems to be the most flexible and resusable solution.

public class AttachCommandBindingsBehavior : Behavior<FrameworkElement>
{
    public ObservableCollection<CommandBinding> CommandBindings
    {
        get
        {
            return (ObservableCollection<CommandBinding>)GetValue(CommandBindingsProperty);
        }
        set
        {
            SetValue(CommandBindingsProperty, value);
        }
    }
    public static readonly DependencyProperty CommandBindingsProperty = DependencyProperty.Register("CommandBindings", typeof(ObservableCollection<CommandBinding>), typeof(AttachCommandBindingsBehavior), new PropertyMetadata(null, OnCommandBindingsChanged));

    private static void OnCommandBindingsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        AttachCommandBindingsBehavior attachCommandBindingsBehavior = (AttachCommandBindingsBehavior)sender;

        if (attachCommandBindingsBehavior == null)
            return;

        ObservableCollection<CommandBinding> commandBindings = (ObservableCollection<CommandBinding>)e.NewValue;

        if (commandBindings != null)
        {
            if (attachCommandBindingsBehavior.CommandBindings != null)
                attachCommandBindingsBehavior.CommandBindings.CollectionChanged -= attachCommandBindingsBehavior.CommandBindings_CollectionChanged;

            attachCommandBindingsBehavior.CommandBindings.CollectionChanged += attachCommandBindingsBehavior.CommandBindings_CollectionChanged;
        }
    }

    void CommandBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ObservableCollection<CommandBinding> collection = (ObservableCollection<CommandBinding>)sender;

        if (collection != null)
        {
            foreach (CommandBinding commandBinding in collection)
                AssociatedObject.CommandBindings.Add(commandBinding);
        }
    }
}

在 xaml 中,您可以这样做:

In xaml you can then do this:

<i:Interaction.Behaviors>
    <localBehaviors:AttachCommandBindingsBehavior CommandBindings="{Binding CommandBindings}"/>
</i:Interaction.Behaviors>

这篇关于如何在 MVVM 中使用 ApplicationCommands的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆