WPF - 在 ViewModel 中处理 ApplicationCommand [英] WPF - Handle an ApplicationCommand in the ViewModel

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

问题描述

我敢打赌,这个问题已经回答了很多次了,但是......

I bet this has been answered many times over, but...

对于 UserControl 上的按钮的命令属性设置为 Find (ApplicationCommands.Find) 之类的简单情况,ViewModel 将如何处理该命令?我通常会看到命令处理程序与添加到 UIElement 上的 CommandBindings 集合中的 CommandBinding 连接,但我的 ViewModel 不是从 UIElement 派生的(应该吗?).命令本身不会公开事件以在执行时进行通知,那么我应该在哪里连接以获取该信息?

For a simple situation where a button on a UserControl has its command property set to something like Find (ApplicationCommands.Find) how would the ViewModel handle that command? I usually see command handlers wired up with a CommandBinding that gets added to a CommandBindings collection on a UIElement, but my ViewModel doesn't derive from UIElement (should it?). The commands themselves don't expose events to notify when they've been executed, so where should I wire up to get that information?

如果可能,我想使用股票 WPF 来解决问题.我知道这类事情有很多可用的框架,但我想保持代码简单.

I'd like to use stock WPF to solve the problem if possible. I know there are many available frameworks for this sort of thing but would like to keep the code simple.

包括一些示例代码.

<UserControl>
  <UserControl.DataContext>
    <local:MyViewModel/>
  </UserControl.DataContext>

  <Button Command="Find"/>
</UserControl>

地点:

class MyViewModel
{
  // Handle commands from the view here.
}

我可以向 UserControl 添加一个 CommandBinding 来处理 Executed,然后在 MyViewModel 中调用一个假设的 Find 方法来执行实际工作,但这是额外且不必要的代码.如果 ViewModel 本身处理 Find 命令,我更喜欢.一种可能的解决方案是让 MyViewModel 从 UIElement 派生,但这似乎违反直觉.

I could add a CommandBinding to the UserControl which would handle Executed, then call a hypothetical Find method in MyViewModel which does the actual work, but that's extra and unnecessary code. I'd prefer if the ViewModel itself handled the Find command. One possible solution would be to have MyViewModel derive from UIElement however that seems counter intuitive.

推荐答案

命令的目的是将生成订单的代码与执行它的代码解耦.因此:如果你想要紧耦合,你最好通过事件来实现:

The purpose of commands is to decouple the code which generates the order from the code which executes it. Therefore: if you want tight coupling, you should better do it through events:

<UserControl ... x:Class="myclass">
    ...
    <Button Click="myclass_find" .../>
    ...
</UserControl>

<小时>

对于松散耦合,您需要将 CommandBinding 添加到您的 UserControl:

<UserControl ... >
    <UserControl.DataContext>
        <local:MyViewModel/>
    </UserControl.DataContext>

    <UserControl.CommandBindings>
        <Binding Path="myFindCommandBindingInMyViewModel"/>
    </UserControl.CommandBindings>
    ...
    <Button Command="ApplicationComamnd.Find" .../>
    ...
</UserControl>

(不确定语法)

或者您可以在构造函数中将 CommandBinding 添加到 UserControlCommandBindings 中,并从 ViewNodel 中获取值:

Or you can add a CommandBinding to your UserControl's CommandBindings in the constructor, taking the value from the ViewNodel:

partial class MyUserControl : UserControl
{
    public MyUSerControl()
    {
        InitializeComponent();
        CommandBinding findCommandBinding = 
                  ((MyViewModel)this.DataContext).myFindCommandBindingInMyViewModel;
        this.CommandBindings.Add(findCommandBinding);
    }
}

这篇关于WPF - 在 ViewModel 中处理 ApplicationCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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