给按钮点击事件添加参数 [英] Add parameter to Button click event

查看:43
本文介绍了给按钮点击事件添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 wpf 按钮:

I have a wpf button like this:

<Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>

我想将 {Binding Code} 作为参数传递给 button1_click 处理程序.
我该怎么做?

And I want to pass {Binding Code} passed as parameter to the button1_click handler.
How do I go about this?

免责声明:WPF 的新手

推荐答案

简单的解决方案:

<Button Tag="{Binding Code}" ...>

在您的处理程序中,将 sender 对象转换为 Button 并访问 Tag 属性:

In your handler, cast the sender object to Button and access the Tag property:

var myValue = ((Button)sender).Tag;

<小时>

更优雅的解决方案是使用 WPF 的命令模式:为您希望按钮执行的功能创建一个命令,将命令绑定到按钮的 Command 属性并将 CommandParameter 绑定到您的值.


A more elegant solution would be to use the Command pattern of WPF: Create a Command for the functionality you want the button to perform, bind the Command to the Button's Command property and bind the CommandParameter to your value.

这篇关于给按钮点击事件添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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