开发者

DependencyProperty string, onChange while typing

开发者 https://www.devze.com 2023-02-05 08:11 出处:网络
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.

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开发者_StackOverflow).SearchTracks(e.NewValue.ToString());
        }

Thanks!


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


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.


Try using PreviewTextInput instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消