Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperty string, onChange while typing

Tags:

c#

search

xaml

I want to create a simple searchbox, so I have a textbox and when someone types a searchterm I want to execute the search method.

The problem is that the onChange method executes when I change click out of the textbox and I want the onChange event executed while I am typing.

<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());
        }

Thanks!

like image 268
BvdVen Avatar asked Jan 27 '26 03:01

BvdVen


2 Answers

<TextBox Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged}" />
like image 50
Euphoric Avatar answered Jan 29 '26 19:01

Euphoric


You must change the UpdateSourceTrigger attribute to ProperyChanged.

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

If you also want to track special keys, you have to register to the PreviewKeyDown-Event.

like image 22
HCL Avatar answered Jan 29 '26 19:01

HCL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!