How can I raise a parent event from a user control in WPF?

我们两清 提交于 2019-12-24 08:23:08

问题


I have a user control that is loaded into the parent window, and I want to raise a parent event when a button on the user control is clicked. How can I communicate with the parent through my user control in WPF?


回答1:


First way that comes to mind is to have your UserControl raise an event that the parent window is listening to. The parent can then raise the event you want.




回答2:


You're looking for Routed Events.

The Click event on Button is a RoutedEvent, so if your UserControl has a button then you can handle that event in the window. Add an attribute ButtonBase.Click="Window_Click" to the Window in your XAML and a handler private void Window_Click(object sender, RoutedEventArgs e) in the code-behind, and it will get called when any button in the window is clicked.



来源:https://stackoverflow.com/questions/3065256/how-can-i-raise-a-parent-event-from-a-user-control-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!