I have a number of text boxes that are dynamically created via code.
我有许多通过代码动态创建的文本框。
I would like to be able to assign a generic event handler to all the textboxes for the text changed even and then within the handler determine which text box has fired the event.
我希望能够为所有文本框分配一个通用事件处理程序,以便更改文本,然后在处理程序中确定哪个文本框已触发事件。
Code I have is:
我的代码是:
txtStringProperty.TextChanged += TextBoxValueChanged;
private void TextBoxValueChanged(object sender, RoutedEventArgs e)
{
string propertyName = // I would like the name attribute of the textbox here
}
Please let me know if you require anymore information.
如果您需要更多信息,请告诉我。
3 个解决方案
#1
7
The sender
parameter contains which control has fired the event. You can cast it to a TextBox and get the name property from it:
sender参数包含触发事件的控件。您可以将其强制转换为TextBox并从中获取name属性:
string propertyName = ((TextBox)sender).Name;
#2
2
Cast object sender
(your textbox which fired event) to TextBox
.
将对象发送者(您触发事件的文本框)转换为TextBox。
If only one property is what you want then write
如果只有一个属性是你想要的那么写
string propertyName = ((TextBox)sender).Name;
But when more than one property is required, then it is better to create a Textbox variable and use it like.
但是当需要多个属性时,最好创建一个Textbox变量并使用它。
TextBox txtbox = (TextBox)sender;
Then you can use any property of it like
然后你可以使用它的任何属性
string propertyName = txtbox.Name;
MessageBox.Show(proptertyName);
MessageBox.Show(txtbox.Content.ToString());
#3
0
My advice is to look at the base class hierarchy at MSDN and just cast the control to it and extract the properties defined on it:
我的建议是在MSDN上查看基类层次结构,然后将控件转换为它并提取在其上定义的属性:
var name = ((ContentControl) sender).Name;
this is also a good practice for a more generic implementation because casting it to 'TextBox' means you can apply the handling logic to that type of control only.
对于更通用的实现,这也是一种很好的做法,因为将其转换为“TextBox”意味着您只能将处理逻辑应用于该类型的控件。
#1
7
The sender
parameter contains which control has fired the event. You can cast it to a TextBox and get the name property from it:
sender参数包含触发事件的控件。您可以将其强制转换为TextBox并从中获取name属性:
string propertyName = ((TextBox)sender).Name;
#2
2
Cast object sender
(your textbox which fired event) to TextBox
.
将对象发送者(您触发事件的文本框)转换为TextBox。
If only one property is what you want then write
如果只有一个属性是你想要的那么写
string propertyName = ((TextBox)sender).Name;
But when more than one property is required, then it is better to create a Textbox variable and use it like.
但是当需要多个属性时,最好创建一个Textbox变量并使用它。
TextBox txtbox = (TextBox)sender;
Then you can use any property of it like
然后你可以使用它的任何属性
string propertyName = txtbox.Name;
MessageBox.Show(proptertyName);
MessageBox.Show(txtbox.Content.ToString());
#3
0
My advice is to look at the base class hierarchy at MSDN and just cast the control to it and extract the properties defined on it:
我的建议是在MSDN上查看基类层次结构,然后将控件转换为它并提取在其上定义的属性:
var name = ((ContentControl) sender).Name;
this is also a good practice for a more generic implementation because casting it to 'TextBox' means you can apply the handling logic to that type of control only.
对于更通用的实现,这也是一种很好的做法,因为将其转换为“TextBox”意味着您只能将处理逻辑应用于该类型的控件。