Abh*_*bhi 4 .net c# wpf events event-bubbling
我是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
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9321 次 |
最近记录: |