WPF C# 以编程方式添加事件处理程序 [英] WPF C# adding event handler programmatically
本文介绍了WPF C# 以编程方式添加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我创建了一个 TextBox 数组:
In my code I create an array of TextBoxes :
namespace TCalc
{
public partial class MainWindow : Window
{
public TextBox[] pubAltArray;
public MainWindow()
{
InitializeComponent();
pubAltArray = new TextBox[10];
然后我使用以下代码以编程方式创建文本框:
Then I create the TextBoxes programmatically using the following code :
private void generatePublishedTxtBox()
{
for (int i = 0; i < 10; i++)
{
TextBox pubAlt = new TextBox();
grid_profile.Children.Add(pubAlt);
pubAlt.SetValue(Grid.RowProperty, 1);
...
pubAltArray[i] = pubAlt;
}
}
当每个 TextBox 的内容发生变化时,我想运行一些例程:
Than I have some routine I want to run when the content of each TextBox changes :
private void doTheStuff(object sender, TextChangedEventArgs e)
{
...
}
所以我尝试在定义新 TextBox 的过程中添加事件处理程序,但没有成功:
So I tried to add event handler during the definition of new TextBox however without success :
pubAlt.TextChanged += new System.EventHandler(doTheStuff());
或
pubAlt.TextChanged += RoutedEventHandler(calculateCorAlts());
对我有什么提示吗?
推荐答案
尝试:
pubAlt.TextChanged += new TextChangedEventHandler(doTheStuff);
或:
pubAlt.TextChanged += doTheStuff;
两条线做同样的事情.第二个只是第一行的简写,因为它使代码更易于阅读.
Both lines do the same thing. The second one is just shorthand for the first line since it makes code easier to read.
这篇关于WPF C# 以编程方式添加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文