WPF:绑定到代码中的命令 [英] WPF: Binding to commands in code behind

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

问题描述

我有一个WPF Microsoft Surface应用程序,我正在使用MVVM模式。

I have a WPF Microsoft Surface Application and I'm using MVVM-Pattern.

我有一些按钮在代码中创建,我想绑定命令,但我只知道如何工作在XAML

I have some buttons that are created in code behind and I would like to bind commands to them, but I only know how that works in the XAML

像这样:

<Custom:SurfaceButton Command="{Binding SaveReservationCommandBinding, Mode=OneWay}"/> 

但我不能这样做,因为我的按钮不存在于XAML,

But I cannot do it like this because my buttons do not exist in the XAML, only in the code behind.

那么在代码中的命令绑定如何工作呢?

So how would a command binding like that works in code behind?

推荐答案

假设你有一个命名的SurfaceButton为SurfaceButton1,并且你可以访问命令的一个实例,你可以使用下面的代码:

Assuming that you have a named your SurfaceButton to "SurfaceButton1" and you have access to an instance of the command, you can use the following code:

SurfaceButton1.Command = SaveReservationCommand;

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

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