WPF - 在父级中处理特定控件实例的 RoutedEvent
在我的用户控件中,我有几个文本框,例如 Textbox1、Textbox2。
在父级中,我只想处理从 Textbox1 引发的 KeyDown 事件,并非所有 Textbox
我都在父级中添加了处理程序
this.AddHandler(TextBox.KeyDownEvent, new RoutedEventHandler(OnTextboxGoToPageKeyDown));
,但当然这将处理我不想要的所有 KeyDown 事件。
我是否必须在 RoutedEventHandler 中添加一些条件逻辑来检查事件的引发位置?
或者有更好的方法吗?
最好的方法是什么?
谢谢。
In my user control I have several textboxes e.g. Textbox1, Textbox2.
In the parent I only want to handle KeyDown events raised from Textbox1, not all Textbox
I have added a handler in the parent
this.AddHandler(TextBox.KeyDownEvent, new RoutedEventHandler(OnTextboxGoToPageKeyDown));
but of course this will handle all KeyDown events which I don't want.
Do I have to add some conditional logic in the RoutedEventHandler to check where the event was raised from?
or is there a better way?
What's the best way to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OnTextboxGoToPageKeyDown
中的RoatedEventArgs
有一个名为Source
的属性,该属性包含对引发事件的对象的引用。当您需要执行逻辑时,您可以请求该属性。华泰
The
RoutedEventArgs
in yourOnTextboxGoToPageKeyDown
has a property namedSource
which have a references to the Object who raised the event. You can ask for that property when you need to execute your logic.HTH