开发者

DependencyProperty string, onChange while typing

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜