Silverlight事件之路由事件
<Grid x:Name="LayoutRoot" Background="White">
<Canvas x:Name="parentCanvas" Background="AliceBlue" MouseLeftButtonDown="parentCanvas_MouseLeftButtonDown">
<Rectangle x:Name="rect" Canvas.Top="50" Canvas.Left="120" Fill="Gold" Stroke="Black" StrokeThickness="3" Width="400" Height=" 160" MouseLeftButtonDown="rect_MouseLeftButtonDown"/>
<TextBlock x:Name="tbkRect" Canvas.Top="100" Canvas.Left="240" FontSize="30" Text="路由事件" Foreground="Black"/>
<TextBlock x:Name="tbkTip" Canvas.Top="220" Canvas.Left="200" FontSize="30"/>
</Canvas>
</Grid>
<Canvas x:Name="parentCanvas" Background="AliceBlue" MouseLeftButtonDown="parentCanvas_MouseLeftButtonDown">
<Rectangle x:Name="rect" Canvas.Top="50" Canvas.Left="120" Fill="Gold" Stroke="Black" StrokeThickness="3" Width="400" Height=" 160" MouseLeftButtonDown="rect_MouseLeftButtonDown"/>
<TextBlock x:Name="tbkRect" Canvas.Top="100" Canvas.Left="240" FontSize="30" Text="路由事件" Foreground="Black"/>
<TextBlock x:Name="tbkTip" Canvas.Top="220" Canvas.Left="200" FontSize="30"/>
</Canvas>
</Grid>
private void parentCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point pnt = e.GetPosition(null);
tbkTip.Text = string.Format("X:{0},Y:{1}",pnt.X.ToString(),pnt.Y.ToString());
}
private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;//添加这一行代码就不会产生路由事件,Handled的默认值为false,即会产生路由事件,所以要想产生路由事件就把改行代码注释掉,或设置为false
tbkRect.Text = "引发路由事件";
}
{
Point pnt = e.GetPosition(null);
tbkTip.Text = string.Format("X:{0},Y:{1}",pnt.X.ToString(),pnt.Y.ToString());
}
private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;//添加这一行代码就不会产生路由事件,Handled的默认值为false,即会产生路由事件,所以要想产生路由事件就把改行代码注释掉,或设置为false
tbkRect.Text = "引发路由事件";
}