简体   繁体   English

WPF如何将同一事件处理程序绑定到两个不同的控件

[英]WPF How to bind the same event handler to two different controls

I have a UI with a search text box and a button that that should be clicked when the user want to preform the search. 我有一个带有搜索文本框和一个按钮的UI,当用户要执行搜索时应单击该按钮。 (like a search engine UI) (如搜索引擎用户界面)
I want that the same event handler will be called when the user hit the search button and when the user hit enter in the text box. 我希望当用户单击搜索按钮和在文本框中单击Enter时将调用相同的事件处理程序。

I can easily hack it but my guess is that WPF has it's own 'right' way of doing it. 我可以很容易地破解它,但是我猜想WPF拥有自己的“正确”方式。
So what is the WPF way of doing it right? 那么WPF正确的做法是什么?

Thanks. 谢谢。

There are different delegates for click and keypressed events. 点击和按键事件有不同的代表。 So extract your code in method named like 'DoSearch', then connect different (mb anonymous) handlers to events and call DoSearch inside handlers 因此,请使用名为“ DoSearch”的方法提取代码,然后将不同的(mb匿名)处理程序连接到事件,并在处理程序中调用DoSearch

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF:如何为具有相同动画和不同事件处理程序的多个控件有效地编写故事板? - WPF: How to write efficiently a storyboard for multiple controls with the same animation and different event handler for `completed`? 如何在wpf上的两个不同窗口中绑定两个控件? - How can I bind two controls in two different windows on wpf? 为不同的Windows窗体控件使用相同的事件处理程序是否安全? - Is it safe to use the same event handler for different Windows Forms Controls? 如何将WPF控件属性与ViewModels不同属性动态绑定? - How to dynamically bind a WPF Controls property with ViewModels different property? 如何将不同类中的两个 Switch 控件绑定到同一个 bool 属性? - How to bind two Switch controls in separate classes to the same bool property? 使用不同的事件处理程序委托为多个控件实现单个事件处理程序 - Implementing a single event handler for multiple controls with different event handler delegates 两个事件的相同事件处理程序需要不同的签名 - Same event handler for two events which requires different signature 如何使用通过lambda表达式创建的同一事件处理程序来订阅两个不同的事件? - How to subscribe to two different events using the same event handler created with a lambda expression? 在不同的事件处理程序中引用以编程方式添加的控件? - referencing programatically added controls in a different event handler? 如何在WPF用户控件的两个不同控件中更改字体大小? - How to change the font size in two different controls of WPF usercontrol?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM