Enablilng dragging in Canvas, v3.5 edition

About a year ago I posted a class implemented couple of dependency properties you could attach to a Canvas and it's children and allowed dragging. The DPs were working quite well with any visual element inside the Canvas, including the standard controls like Button.

Unfortunately, with .Net v3.5 the attached behavior was broken due to new behavior of some of the controls. For example, the Button control now supports three different OnClick behaviors - on hover, on press and on release. My DP code depends on the MouseLeftButtonDown event being fired; however, the new Button behavior was suppressing this event.

Fortunately, the fix for this is quite easy. Instead of hooking the MouseLeftButtonDown, MouseLeftButtonUp and MouseMove events, I now hook the PreviewMouseLeftButtonDown, PreviewMouseLeftButtonUp and PreviewMouseMove events. This allows my code to enable dragging on Buttons, without modifying their behavior.

Add a Comment