Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - WPF mouse message handling

 
 
Old 04-10-2007   #1 (permalink)
mathieu_gdal@yahoo.fr


 
 

WPF mouse message handling

Dear all,

I wonder how works WPF with mouse events. It is a question about the
WPF internal mechanics.

For example, I have a canvas containing a listbox containing a button.
How is handled a mouse click?
1- Is there an internal "click" method that is recursively called down
the hierarchy? (canvas.Click calls listbox.click that calls
button.Click...)
2- Or is there an object that knows the list of all visuals (and their
positions), and calls exactly the right visual?
3- Or even another pattern?

Thanks
-Mat


My System SpecsSystem Spec
Old 04-11-2007   #2 (permalink)
Plamen Ratchev


 
 

Re: WPF mouse message handling

WPF uses routed events, which are an enhanced version of the standard .NET
event-handling model. Instead of just calling handlers that are attached to
the element which raised the event, the routed events call all of the
attached handlers (to any node), for the originating element to the root of
the user interface tree.

The routed events can be bubbling, tunneling, or direct. Bubbling events
start looking for event handlers attached to the element, and then to the
parent, and so on until the root. Tunneling events work in reverse. They
look for handlers at the root and then work down to the originating element.
Direct events are routed the same way as the standard .NET events. Only the
directly attached handlers are notified.

WPF defines most routed events in pairs (one bubbling and one tunneling),
except for direct events. The tunneling events are named with prefix Preview
and raised first.

Here is an example of a few mouse events:

Event -> Routing
--------------------
MouseEnter -> Direct
MouseLeave -> Direct
PreviewMouseLeftButtonDown -> Tunnel
MouseLeftButtonDown -> Bubble

The UIElement defines two properties to identify if the mouse cursor is over
the element - IsMouseOver and IsMouseDirectlyOver.

As for Click it is a little bit different. Because clicks can be initiated
by either the mouse or the keyboard, they are handled at higher level. So,
different elements at higher level define events for Click. For example, the
Control class has MouseDoubleClick and PreviewMouseDoubleClick; the further
the ButtonBase adds the Click event.

In your example, when the mouse is over the button and a click is
registered, the Click event of the button will be activated. Then it will be
handled based on what type of button is, if you have defined Click event
handler, or you could have a defined Command that will be invoked.

You could also use an attached event. That means if you do not define a
handler for Click at the Button level, the Click will bubble to the parent,
and so on to the root. Even if element at a higher level does not define its
own Click event, you can handle it at that level. The event attribute just
has to be prefixed with the class name defining the Click event (like
Button.Click="Button_Click").

Hope this is not too confusing. Best is to create a simple application
with a few controls and to play with events at different levels. WPF adds a
lot more power to what can be done with events. And most of the time there
is no need of writing event handlers. For example, styles with property and
event triggers can handle it well for visual effects.

HTH,

Plamen Ratchev
http://www.SQLStudio.com


My System SpecsSystem Spec
Old 04-13-2007   #3 (permalink)
mathieu_gdal@yahoo.fr


 
 

Re: WPF mouse message handling

Thanks a lot for this helpful answer!
-Mathieu

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
WMI Error Handling PowerShell
Error Handling PowerShell
Handling events from COM? PowerShell
IE7 and mouse message Vista security
Possible Enhancements to XML Handling for V2 PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46