none
Button的PreviewMouseLeftButtonDown设置e.Handler = true后,使用AddHanlder添加事件无效 RRS feed

  • 问题

  •  Hi,

    我在使用AddHandler时,遇到了如题问题。代码如下: 

          
     

    public MainWindow() { InitializeComponent(); TestBt.PreviewMouseLeftButtonDown += TestBt_OnPreviewMouseLeftButtonDown; TestBt.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Button_Click), true); TestBt.AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(TestBt_MouseDown), true); } private void TestBt_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { System.Diagnostics.Debug.WriteLine("TestBt_OnPreviewMouseLeftButtonDown");

    e.Handled = true;

    } private void Button_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Debug.WriteLine("Button_Click"); } private void TestBt_MouseDown(object sender, MouseButtonEventArgs e) { System.Diagnostics.Debug.WriteLine("TestBt_MouseDown"); } }

    为什么Button_Click,TestBt_MouseDown仍然被截断呢?

    2014年12月16日 7:18

答案

  • 你好 Kevin,

    你的意思是后两个事件被之前的OnPreviewMouseLeftButtonDown事件截断了是吧?

    关于这个MSDN有解释,看这里吧:

    http://msdn.microsoft.com/zh-cn/library/ms742806(v=vs.110).aspx

    “Routed events use one of three routing strategies:          
    • 冒泡:针对事件源调用事件处理程序。" data-guid="ca129545-da6b-fad2-48d2-934babd82153" id="mt43" xml:space="preserve">Bubbling: Event handlers on the event source are invoked.  The routed event then routes to successive parent elements until reaching the element tree root.  Most routed events use the bubbling routing strategy.  Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements. 

    • 直接:只有源元素本身才有机会调用处理程序以进行响应。" data-guid="b9f76027-b0fa-1c3a-56a1-110e40c00a91" id="mt47" xml:space="preserve">Direct: Only the source element itself is given the opportunity to invoke handlers in response.  This is analogous to the "routing" that Windows Forms uses for events.  EventSetterEventTrigger 使用。" data-guid="d520faf0-4b19-e683-9756-2b53df75c6f3" id="mt49" xml:space="preserve">However, unlike a standard CLR event, direct routed events support class handling (class handling is explained in an upcoming section) and can be used by EventSetter and EventTrigger

    • 隧道:最初将在元素树的根处调用事件处理程序。" data-guid="05beaaa6-c0a0-6756-fbeb-8ff998af2032" id="mt50" xml:space="preserve">Tunneling: Initially, event handlers at the element tree root are invoked.  The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event).  Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control.  Input events provided in WPF often come implemented as a tunneling/bubbling pair.  Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

    隧道路由事件如果被标记为Handled,相应的冒泡事件将不会再发生。

    你还可以看这个文章来做参考:

    http://msdn.microsoft.com/zh-cn/library/ms747183(v=vs.110).aspx

    信息请看 预览隧道事件和冒泡事件对比以及事件处理。我这边贴中文的msdn格式会不太对,你自己对应看一下理解一下吧。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年12月17日 7:46
    版主