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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论