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

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

问题描述

我猜这已经回答了很多次,但...

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

对于一个简单的情况,其中UserControl上的按钮的命令属性设置为类似Find(ApplicationCommands.Find)ViewModel如何处理该命令?我通常看到命令处理程序连接到一个CommandBinding添加到UIElement上的CommandBindings集合,但我的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.

EDIT2:包括一些示例代码。

Including some sample code.

<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>

(不确定语法)

或者您可以向 UserControl CommandBindings CommandBinding c>在构造函数中,从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天全站免登陆