如何绑定在WPF命令 [英] How to Bind a Command in WPF

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

问题描述

有时我们用复杂的方式这么多次,我们忘了做任务最简单的方法。

Sometimes we used complex ways so many times, we forgot the simplest ways to do the task.

我知道该怎么做指挥约束力,但我总是用相同做法。

I know how to do command binding, but i always use same approach.

创建一个实现ICommand接口,并从视图模型我创建该类的新实例,并结合工程就像一个魅力的一类。

Create a class that implements ICommand interface and from the view model i create new instance of that class and binding works like a charm.

这是我用命令绑定

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;            
        testCommand = new MeCommand(processor);
    }

    ICommand testCommand;

    public ICommand test
    {
        get { return testCommand; }
    }
    public void processor()
    {
        MessageBox.Show("hello world");
    }
}

public class MeCommand : ICommand
{
    public delegate void ExecuteMethod();
    private ExecuteMethod meth;
    public MeCommand(ExecuteMethod exec)
    {
        meth = exec;
    }

    public bool CanExecute(object parameter)
    {
        return false;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        meth();
    }
}



但我想知道这样做的基本途径没有第三方dll的任何新类的创建。做这个简单的命令使用单一类绑定。实际的类从ICommand接口实现和完成的工作。

But i want to know the basic way to do this, no third party dll no new class creation. Do this simple command binding using a single class. Actual class implements from ICommand interface and do the work.

推荐答案

Prism 已经提供的 Microsoft.P​​ractices.Prism.Commands.DelegateCommand

我不知道它是作为第三方。至少它的官方并记录在MSDN上。

I'm not sure is it considered as 3rd party. At least it's official and documented on MSDN.

一些原生内置的命令,例如复制,粘贴实现ICommand接口。
恕我直言继打开(用于扩展)/关闭(更改)的原则。这样我们就可以实现我们自己的命令。

Some native build-in commands such copy, paste implements ICommand interface. IMHO it following the Open(for extends)/Close(for changes) principle. so that we can implement our own command.

由于WPF指挥记载这里 ,摘录...

As WPF Commanding documented here, an excerpt...

WPF提供了一组预定义命令。如削,BrowseBack和
BrowseForward,播放,停止和暂停。

WPF provides a set of predefined commands. such as Cut, BrowseBack and BrowseForward, Play, Stop, and Pause.

如果在命令库类的命令不符合您的需求,
,那么你可以创建自己的命令。有两种方法来创建一个
自定义命令。首先是从地面启动和实施
中的ICommand接口。另一种方法,和更常见的做法
是创建的
的RoutedCommand RoutedUICommand

If the commands in the command library classes do not meet your needs, then you can create your own commands. There are two ways to create a custom command. The first is to start from the ground up and implement the ICommand interface. The other way, and the more common approach, is to create a RoutedCommand or a RoutedUICommand.

我试过的RoutedCommand模式在开始和结束了与实施ICommand的。

I've tried RoutedCommand model at the beginning and ended up with implementing ICommand.

样品XAML绑定

<CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />



的RoutedCommand是不是从RoutedEvent不同。这似乎是一个更好的按钮的点击事件处理程序。它服务的宗旨:以单独的应用程序逻辑和观点,但需要一些附加的DependencyProperty或代码隐藏。

RoutedCommand is not different from RoutedEvent. this seems like a better button's 'Clicked' event handler. It serves the purpose: To separate app logic from View but require some attach DependencyProperty or code-behind.

我个人觉得更舒服只有实现我的ICommand的。

personally I feel more comfortable with just implement my ICommand.

这篇关于如何绑定在WPF命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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