本文介绍了如何获取/指定有关 WPF 中以编程方式添加的按钮的信息?或者类似地,如何将自定义 EventArgs 传递给这样的按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我以编程方式向 WPF 应用程序添加了一系列按钮,作为标签、文本框、按钮部分的一部分,并将单个处理程序附加到所有这些的单击事件.如何指定正在单击哪个按钮(与哪个部分有关),以便对其进行相应处理?

Say I added a series buttons to a WPF app progammatically, as part of a label, textbox, button section, and attached a single handler to the click event of all of these. How can I specify which button (pertaining to which section) is being clicked, so it can be handled accordingly?

附加个人处理程序将不起作用,因为用户必须能够根据需要添加尽可能多的这些行".

Attaching individuals handlers won't work since the user must be able to add as many of these 'lines' as needed.

一种可能的替代方法是将信息传递给事件处理程序,例如:

A possible alternative would be to pass information to the event handler, such as:

    ...
    var sender = this;
    var args = new CustomEventArgs(sectionName);

    var button = new Button();
    button.Click += Button_EventHandler_Click(sender, args);

但我还没有在 C# 中找到方法.

But I haven't found a way to this in C#.

任何帮助/想法将不胜感激!

Any help/idea will be appreciated!

谢谢

推荐答案

sender参数,就是被点击的按钮.

Look at the sender parameter, it will be the clicked button.

如果需要进一步区分按钮,可以设置按钮Tag属性.

If you need to further differenciate the buttons, you can set the Tag property of the button.

这篇关于如何获取/指定有关 WPF 中以编程方式添加的按钮的信息?或者类似地,如何将自定义 EventArgs 传递给这样的按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:49