如何:偵測 TextBox 中的文字何時變更
這個範例示範每當控制項中的文字變更時,使用 TextChanged 事件執行方法的 TextBox 其中一種方式。
在 XAML 的程式碼後置類別中,包含 TextBox 您要監視變更的控制項,插入方法以在每次 TextChanged 引發事件時呼叫。 這個方法必須具有符合委派預期 TextChangedEventHandler 專案的簽章。
每當使用者或以程式設計方式變更控制項的內容 TextBox 時,就會呼叫 事件處理常式。
注意
建立控制項並一開始填入文字時, TextBox 就會引發此事件。
定義 TextBox 控制項
在定義控制項 TextBox 的 Extensible Application Markup Language (XAML) 中,以符合事件處理常式方法名稱的值來指定 TextChanged 屬性。
<TextBox TextChanged="textChangedEventHandler">
Here is the initial text in my TextBox. Each time the contents of this TextBox are changed,
the TextChanged event fires and textChangedEventHandler is called.
</TextBox>
監視 TextBox 控制項變更
在 XAML 的程式碼後置類別中,包含 TextBox 您要監視變更的控制項,插入方法以在每次 TextChanged 引發事件時呼叫。 這個方法必須具有符合委派預期 TextChangedEventHandler 專案的簽章。
// TextChangedEventHandler delegate method.
private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
// Omitted Code: Insert code that does something whenever
// the text changes...
} // end textChangedEventHandler
' TextChangedEventHandler delegate method.
Private Sub textChangedEventHandler(ByVal sender As Object, ByVal args As TextChangedEventArgs)
' Omitted Code: Insert code that does something whenever
' the text changes...
End Sub
每當使用者或以程式設計方式變更控制項的內容 TextBox 時,就會呼叫 事件處理常式。
注意
建立控制項並一開始填入文字時, TextBox 就會引發此事件。
註解
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應