将参数添加到Button单击事件 [英] Add parameter to Button click event

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

问题描述

我有一个像这样的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?

免责声明: >

Disclaimer: really new to WPF

推荐答案

简单解决方案:

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

在您的处理程序中,转换 sender 对象到按钮并访问标签属性:

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

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






更优雅的解决方案是使用< a href =http://msdn.microsoft.com/en-us/library/ms771540.aspx =nofollow noreferrer> WPF的命令模式:为您想要的功能创建命令按钮将Command绑定到Button的 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.

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

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