Binding Mouse events to Canvas

Topics: UI Architecture
Jul 26, 2011 at 2: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" 
Height="300"
cal:Message.Attach="[Event MouseRightButtonDown] = [Action DispatchClick()]; [Event MouseRightButtonUp] = [Action Release()]" >
 <Polyline Points="{Binding Points}" Stroke="DarkGray" StrokeThickness="2" />

</Canvas>

and

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

 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 2:56 PM
Edited Jul 26, 2011 at 2: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 3:10 PM

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