DependencyProperty 字符串,键入时 onChange
我想创建一个简单的搜索框,所以我有一个文本框,当有人输入搜索词时我想执行搜索方法。
问题是,当我在文本框中单击更改时,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());
}
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将 UpdateSourceTrigger 属性更改为属性已更改。
如果您还想跟踪特殊键,则必须注册PreviewKeyDown - 事件。
You must change the UpdateSourceTrigger attribute to ProperyChanged.
If you also want to track special keys, you have to register to the PreviewKeyDown-Event.
尝试使用 PreviewTextInput 代替。
Try using PreviewTextInput instead.