views:

153

answers:

2

Hello, A simple silverlight app:

<Grid x:Name="LayoutRoot">
    <Canvas x:Name="C1" MouseLeftButtonDown="C1_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Canvas x:Name="C2" MouseLeftButtonDown="C2_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Rectangle x:Name="R1" Fill="AliceBlue" Height="40" Width="60"/>
        </Canvas>
    </Canvas>
</Grid>

Why do the Canvas mouse event handlers get called only when I click in the Rectangle control, and not in the empty Canvas?
Thanks.

+3  A: 

You need to give the Canvas a background brush to give the Canvas a surface on which to detect a mouse.

<Grid x:Name="LayoutRoot"> 
    <Canvas x:Name="C1" Background="White" MouseLeftButtonDown="C1_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
        <Canvas x:Name="C2" MouseLeftButtonDown="C2_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
            <Rectangle x:Name="R1" Fill="AliceBlue" Height="40" Width="60"/> 
        </Canvas> 
    </Canvas> 
</Grid>
AnthonyWJones
A: 

This background could be also be set to Transparent...

Jordan