TextBox.TextChanging 事件

定義

當文字方塊中的文字開始變更,但在轉譯之前,就會同步發生。

// Register
event_token TextChanging(TypedEventHandler<TextBox, TextBoxTextChangingEventArgs const&> const& handler) const;

// Revoke with event_token
void TextChanging(event_token const* cookie) const;

// Revoke with event_revoker
TextBox::TextChanging_revoker TextChanging(auto_revoke_t, TypedEventHandler<TextBox, TextBoxTextChangingEventArgs const&> const& handler) const;
public event TypedEventHandler<TextBox,TextBoxTextChangingEventArgs> TextChanging;
function onTextChanging(eventArgs) { /* Your code */ }
textBox.addEventListener("textchanging", onTextChanging);
textBox.removeEventListener("textchanging", onTextChanging);
- or -
textBox.ontextchanging = onTextChanging;
Public Custom Event TextChanging As TypedEventHandler(Of TextBox, TextBoxTextChangingEventArgs) 
<TextBox TextChanging="eventhandler"/>

事件類型

範例

此範例示範如何處理 TextChanging 事件,以實作 TextBox的簡單自動完成。

<!-- Text box in MainPage.xaml -->
<TextBox x:Name="textBox" TextChanging="textBox_TextChanging"
         Width="200" Height="32"/>
public sealed partial class MainPage : Page
{
    // Boolean to keep track of whether or not you should ignore the next TextChanged event.  
    // This is needed to support the correct behavior when backspace is tapped.
    public bool m_ignoreNextTextChanged = false;

    // Sample list of strings to use in the autocomplete.
    public string[] m_options = { "microsoft.com", "dev.windows.com", "msn.com", "office.com", "msdn.microsoft.com" };

    public MainPage()
    {
        this.InitializeComponent();
    }

    private void textBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {
        // Needed for the backspace scenario.
        if (m_ignoreNextTextChanged)
        {
            m_ignoreNextTextChanged = false;
            return;
        }
        // All other scenarios other than the backspace scenario.
        // Do the auto complete.
        else
        {
            string s = textBox.Text;
            if (s.Length > 0)
            {
                for (int i = 0; i < m_options.Length; i++)
                {
                    if (m_options[i].IndexOf(s) >= 0)
                    {
                        if (s == m_options[i])
                            break;

                        textBox.Text = m_options[i];
                        textBox.Select(s.Length, m_options[i].Length - s.Length);
                        break;
                    }
                }
            }
        }
    }

    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Back
            || e.Key == Windows.System.VirtualKey.Delete)
        {
            m_ignoreNextTextChanged = true;
        }
        base.OnKeyDown(e);
    }
}

備註

如需事件資料,請參閱 TextBoxTextChangingEventArgs

TextChanging 事件會在轉譯新文字之前同步發生。 相反地, TextChanged 事件是非同步,會在轉譯新文字之後發生。

發生 TextChanging 事件時,Text 屬性已經反映新的值 (,但不會在 UI 中轉譯) 。 您通常會處理此事件,以在轉譯文字之前更新 Text 值和選取範圍。 這可防止文字閃爍,在快速轉譯、更新及重新轉譯文字時發生。

注意

這是不允許 XAML 視覺化樹狀結構變更時發生的同步事件,例如在版面配置期間。 因此,您應該限制 TextChanging 事件處理常式內的程式碼主要是檢查及更新 Text 屬性。 嘗試執行其他動作,例如顯示快顯視窗,或從視覺化樹狀結構新增/移除元素,可能會造成可能導致損毀的潛在嚴重錯誤。 建議您在 TextChanged 事件處理常式中執行這些其他變更,或將它們當做個別的非同步作業來執行。

適用於

另請參閱