如何为TextBox结束编辑注册事件

时间:2015-06-08 21:15:02

标签: c# events winrt-xaml

超级新手到C#(第一天编码),所以如果以下内容真的是一个愚蠢的问题,请不要过多评价我。

但是我正在寻找一种在TextBox结束编辑时注册事件的方法,这在iOS中类似于textFieldDidEndEditing(_ textField: UITextField)委托。

在谷歌搜索后,我知道如何使用以下内容注册textChanged事件:

.xaml 文件中:

<TextBox TextChanged="textChangedEventHandler"/>

.cs 文件中:

protected void textChangedEventHandler(object sender, TextChangedEventArgs args)
{

}

我也注意到这个SO,最后是MS documentation并注意到以下功能:

// This method handles the LostFocus event for textBox1 by setting the  
// dialog's InitialDirectory property to the text in textBox1. 
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
    // ... 
}

但对我来说不明显的是如何注册此事件功能?或者,当TextBox结束编辑时,如何让GUI知道调用此函数?

这最终是它的工作原理:

.xaml 文件中:

<TextBox LostFocus="textFinishedEditingEventHandler"/>

.cs 文件中:

public void textFinishedEditingEventHandler(object sender, RoutedEventArgs e)
{

}

感谢@Dacker!〜

1 个答案:

答案 0 :(得分:1)

通常情况下,可以通过两种方式注册事件:

在标记中。在ASP.Net中,每种类型的事件都以前缀On显示,因此对于Click it的OnClick。在你的xaml中,我没有看到On前缀,所以这让我猜你的情况如下:

<TextBox LostFocus="textBox1_LostFocus" />

在代码后面(.cs)

textBox1.LostFocus += textBox1_LostFocus

如果你理解这一点,你可以使用更好的名称来textBox1_LostFocus来描述更多会发生什么,而不是什么时候发生。