Binding Mouse events to Canvas

Topics: UI Architecture
Jul 26, 2011 at 3:51 PM

I'm trying to bind mouse event to a canvas (on a silverlight app) without much luck.   So far, I've tried:

<Canvas  Width="300" 
cal:Message.Attach="[Event MouseRightButtonDown] = [Action DispatchClick()]; [Event MouseRightButtonUp] = [Action Release()]" >
 <Polyline Points="{Binding Points}" Stroke="DarkGray" StrokeThickness="2" />



<Canvas  Width="300" Height="300">
<Polyline Points="{Binding Points}" Stroke="DarkGray" StrokeThickness="2" />
<i:EventTrigger EventName="MouseLeftButtonDown">
<cal:ActionMessage MethodName="DispatchClick" />
<i:EventTrigger EventName="MouseLeftButtonUp">
<cal:ActionMessage MethodName="Release" />

 Neither seems to have any effect.  Using the first method, if I replace those events with MouseEnter & MouseLeave, the methods are triggered, but own when the mouse crosses over (ie. enter & leaves) the PolyLine.  Eventually, I want to react differently depending on where in the canvas the click occurs, but right now, I'd be happy with any reaction from mouse down/mouse up.



Jul 26, 2011 at 3:56 PM
Edited Jul 26, 2011 at 3:57 PM

I suppose it depends on the fact that the hit test fails.

By default, the Canvas has no background (null), hence no mouse-related event can fire. Try to set the Background to Transparent, and see what happens.

Jul 26, 2011 at 4:10 PM

Excellent!  That worked perfectly!   (I figured it was something dumb like that...)