String DependencyProperty, onChange when typing

I want to create a simple search box, so I have a text box, and when someone enters a search query, I want to execute a search method.

The problem is that the onChange method is executed when I change the click from the text field, and I want the onChange event to be fired during input.

<TextBox Text="{Binding SearchTerm}" />

public static readonly DependencyProperty SearchTermProperty =
            DependencyProperty.Register("SearchTerm", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty, OnCaptionPropertyChanged));
        private static void OnCaptionPropertyChanged(DependencyObject dependencyObject, 
               DependencyPropertyChangedEventArgs e) 
        {
            ((MainWindow)dependencyObject).SearchTracks(e.NewValue.ToString());
        }

Thank!

+3
source share
3 answers
<TextBox Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged}" />
+3
source

You must change the UpdateSourceTrigger attribute in ProperyChanged.

<TextBox Text="{Binding SearchTerm,UpdateSourceTrigger=PropertyChanged}" />

If you also want to track special keys , you need to register with PreviewKeyDown- Event.

+2
source

PreviewTextInput.

0

Source: https://habr.com/ru/post/1786479/