Friday, November 8, 2013

PAFF!


A little more work on the previous example permits a quite powerful result. It turns out the WPF offers the possibility of preview event handlers. In effect, one can define event handlers at any level in the visual tree, coming or going. (The visual tree is not always the same as the logical tree because it is control-specific). Preview events are tunneling events - from the root to the control - as opposed to bubbling events, from the control to the root. See how it works.






And the beauty of the thing is that having another element on top of the rectangle changes nothing. The click, qua event, is ready to execute the first event handler it can find. It will go to the root and find nothing. As it tunnels out, it will encounter the Preview for rectangle as source which is a listening for the click, and execute the fill-with-red command on the pertinent rectangle. As the MSDN library points out, great care needs to be taken in using preview events...

 

No comments: