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에서는 렌더링되지 않습니다. 일반적으로 텍스트가 렌더링되기 전에 텍스트 값 및 선택을 업데이트하기 위해 이 이벤트를 처리합니다. 이렇게 하면 텍스트가 빠르게 렌더링, 업데이트 및 다시 렌더링될 때 발생할 수 있는 텍스트 깜박임이 방지됩니다.

참고

레이아웃 중과 같이 XAML 시각적 트리에 대한 변경이 허용되지 않는 경우에 발생할 수 있는 동기 이벤트입니다. 따라서 TextChanging 이벤트 처리기 내의 코드를 주로 Text 속성 검사 및 업데이트로 제한해야 합니다. 팝업 표시 또는 시각적 트리에서 요소 추가/제거와 같은 다른 작업을 수행하려고 하면 잠재적으로 심각한 오류가 발생하여 충돌이 발생할 수 있습니다. TextChanged 이벤트 처리기에서 이러한 다른 변경 내용을 수행하거나 별도의 비동기 작업으로 실행하는 것이 좋습니다.

적용 대상

추가 정보