依赖属性字符串,实时更改时触发onChange

3

我想创建一个简单的搜索框,因此我有一个文本框,当有人输入搜索词时,我想执行搜索方法。

问题在于,当我从文本框中点击出来时,onChange方法会执行,而我希望在我输入时执行onChange事件。

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

谢谢!

3个回答

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

2

您必须将UpdateSourceTrigger属性更改为PropertyChanged。

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

如果您还想追踪特殊键,您需要注册到“PreviewKeyDown”事件。该事件的注册方法请参考此链接

0

尝试使用 PreviewTextInput 替代。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接