动态创建按钮单击事件 [英] Dynamically creating Button Click Events

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

问题描述

大家好,请帮帮我.

我有一个用户控件,我在放置在列表框项目模板中的home.xaml中使用了该控件.它像我的表一样动态创建时间.

我的问题是:

我需要动态创建按钮单击事件,在其中可以显示一个消息框,其中包含与特定按钮单击相关的数据

我提出了一个解决方案,例如:

Hello guys please help me in this.

I have a usercontrol I am using that in my home.xaml which is placed in listbox item template. It dynamically creates number of time as in my table.

My question is:

I need to create dynamically button click event where I can display a messagebox which contain data related to that particular button click

I came up a solution like:

public  usercontrol()
{
    InitializeComponent();
    testButton.Click +=(s,e) =>
        {
             if(buttonclick != null )
                 buttonclick(s,e);
        };
...


您能否向我解释在单击事件的方式和位置,我们可以编写消息框?

谢谢!


Can you please explain to me how and where in the event click we can write messagebox?

Thank you!

推荐答案

为什么不只是使用一种更易于维护的机制. Lamda表达式令人混淆,并不总是一个好主意.

Why don''t you just use a more maintainable mechanism. Lamda expressions are obfuscatory and aren''t always a good idea.

...
testButton.Click += new EventArgs(button_Click);
...
<br />
private void buttonClick(object sender, EventArgs e)
{
  // do whatever you need to have done
}


为什么您不只是使用一种更易于维护的机制. Lamda表达式令人混淆,并不总是一个好主意.

Why don''t you just use a more maintainable mechanism. Lamda expressions are obfuscatory and aren''t always a good idea.

...
testButton.Click += new EventArgs(button_Click);
...
<br />
private void buttonClick(object sender, EventArgs e)
{
  // do whatever you need to have done
}


嘿,我得到了答案,感谢所有想到这个问题的人,这就是答案


公共Usercontrol()
{
InitializeComponent();
ButtonExport.Click + =新的RoutedEventHandler(ButtonExport_Click);
}

void ButtonExport_Click(对象发送者,RoutedEventArgs e)
{
MessageBox.Show(TextHeader.Text);
}
hey guys I got the answer thanks for all the guys who ever was thinking of this question and here is the answer


public Usercontrol()
{
InitializeComponent();
ButtonExport.Click += new RoutedEventHandler(ButtonExport_Click);
}

void ButtonExport_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(TextHeader.Text);
}


这篇关于动态创建按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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