简单的silverlight鼠标事件问题

时间:2010-01-06 23:50:24

标签: silverlight silverlight-3.0

一个简单的silverlight应用程序:

<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>

为什么Canvas鼠标事件处理程序仅在我单击Rectangle控件时调用,而不是在空Canvas中调用?
感谢。

1 个答案:

答案 0 :(得分:4)

您需要为Canvas提供背景画笔,以便为Canvas提供一个可以检测鼠标的表面。

<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>