加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF应用程序中的事件冒泡

发布时间:2020-12-16 01:44:59 所属栏目:百科 来源:网络整理
导读:我是 WPF的新手.在我的 WPF应用程序中,我有 Windows,其中包含用户定义的子控件??,用户定义的子控件??再次包含另一个用户定义的子控件??.现在从最内层的子控件,点击一下按钮,我想在所有三个控件上启动事件(即First Grand Child Control,Second Child Control,
我是 WPF的新手.在我的 WPF应用程序中,我有 Windows,其中包含用户定义的子控件??,用户定义的子控件??再次包含另一个用户定义的子控件??.现在从最内层的子控件,点击一下按钮,我想在所有三个控件上启动事件(即First Grand Child Control,Second Child Control,Third Main Control和Window).

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

解决方法

最重要的部分代码为:
在静态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
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读