确定哪个文本框已触发文本更改事件

use*_*195 4 c# wpf event-handling

我有许多通过代码动态创建的文本框.

我希望能够为所有文本框分配一个通用事件处理程序,以便更改文本,然后在处理程序中确定哪个文本框已触发事件.

我的代码是:

txtStringProperty.TextChanged += TextBoxValueChanged;

private void TextBoxValueChanged(object sender, RoutedEventArgs e)
{
    string propertyName = // I would like the name attribute of the textbox here
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请告诉我.

Sjo*_*erd 7

sender参数包含触发事件的控件.您可以将其强制转换为TextBox并从中获取name属性:

string propertyName = ((TextBox)sender).Name;
Run Code Online (Sandbox Code Playgroud)