WPF应用程序中的事件冒泡

我是WPF的新手。 在我的WPF应用程序中,我有Windows,其中包含用户定义的子控件​​,用户定义的子控件​​再次包含另一个用户定义的子控件​​。 现在从最内层的儿童控制,点击按钮,我想在所有三个控件(即第一个大孩子控制,第二个孩子控制,第三个主控制和窗口)上触发事件。

我知道这可以通过代表和事件冒泡来实现。 你能告诉我怎么样吗?

最重要的部分pf代码:在静态UIElement.MouseLeftButtonUpEvent上添加事件处理程序:

middleInnerControl.AddHandler(UIElement.MouseLeftButtonUpEvent , new RoutedEventHandler(handleInner)); //adds the handler for a click event on the most out mostOuterControl.AddHandler(UIElement.MouseLeftButtonUpEvent , new RoutedEventHandler(handleMostOuter)); //adds the handler for a click event on the most out 

EventHandlers:

 private void handleInner(object asd, RoutedEventArgs e) { InnerControl c = e.OriginalSource as InnerControl; if (c != null) { //do whatever } e.Handled = false; // do not set handle to true --> bubbles further } private void handleMostOuter(object asd, RoutedEventArgs e) { InnerControl c = e.OriginalSource as InnerControl; if (c != null) { //do whatever } e.Handled = true; // set handled = true, it wont bubble further } 

看看这个http://msdn.microsoft.com/en-us/library/ms742806.aspx

此页面解释了有关路由事件的所有信息,包括如何实现和使用它们。