一、冒泡路由事件
实例演示事件的冒泡过程。当单击标签中的一部分时,在列表框中会显示事件发生的顺序。
XAML标记代码:
<Window x:Class="WpfApplication5_2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication5_2"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="450"
WindowStartupLocation="CenterScreen"
MouseUp="SomethingClicked">
<Grid Margin="5" MouseUp="SomethingClicked">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label MouseUp="SomethingClicked" Margin="5" Grid.Row="0" HorizontalAlignment="Left"
Background="AliceBlue" BorderThickness="1" BorderBrush="Black">
<StackPanel MouseUp="SomethingClicked">
<TextBlock MouseUp="SomethingClicked" Margin="3">Image and picture label</TextBlock>
<Image MouseUp="SomethingClicked" Source="happyface.jpg" Stretch="None"></Image>
<TextBlock MouseUp="SomethingClicked" Margin="3">Courtesy of the StackPanel</TextBlock>
</StackPanel>
</Label>
<ListBox x:Name="lstMessages" Grid.Row="1" Margin="5"></ListBox>
<CheckBox x:Name="chkHandle" Grid.Row="2" Margin="5">Handle first Event</CheckBox>
<Button x:Name="cmdClear" Grid.Row="3" Margin="5" Padding="3" HorizontalAlignment="Right" Click="cmdClear_Click">Clear List</Button>
</Grid>
</Window>
CS后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication5_2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int eventCounter = 0;
private void SomethingClicked(object sender, RoutedEventArgs e)
{
eventCounter++;
string message = "#" + eventCounter.ToString() + ":\r\n" +
"Sender:" + sender.ToString() + ":\r\n" +
"Source:" + e.Source.ToString() + ":\r\n" +
"Original Source:" + e.OriginalSource.ToString();
lstMessages.Items.Add(message);
e.Handled = (bool)chkHandle.IsChecked;
}
private void cmdClear_Click(object sender, RoutedEventArgs e)
{
eventCounter = 0;
lstMessages.Items.Clear();
}
}
}
注意:在此需要一次额外的强制转换,因为CheckBox.IsChecked属性是可空的Boolean值。空值表示复选框的当前状态尚未确定。这意味着既不是选中状态也不是未选中状态。该例中未使用这一特性,所以使用简单的强制转换来解决这一问题。
显示效果如下:
当单击标签内的文本时显示效果如下:
当勾选Handle first Event复选框后,再单击标签内的文本时显示效果如下:
当勾选复选框后,SomethingClicked()方法就将RoutedEventArgs.Handled属性设为true,从而在事件第一次发生时就终止了事件的冒泡过程。因此,在上图列表框中就只能看到第一个事件。
注意:在SomethingClicked()方法的第二个参数用RoutedEventArgs对象,而没有使用MouseUp事件提供的MouseButtonEventArgs对象(该对象具有在事件发生时有关鼠标状态的附加信息)。这是因为MouseButtonEventArgs对象继承自MouseEventArgs类,而MouseEventArgs类又继承自RoutedEventArgs类。所以,如果不需要与鼠标相关的附加信息,在声明事件处理程序时可使用RoutedEventArgs类(就像本例一样)。
为什么单击按钮没有引发Grid和窗体的MouseUp事件,这是因为按钮中包含了一些有趣的代码,这些代码会挂起MouseUp事件,并引发更高级的Click事件。同时,Handle标志被设置为true,从而会阻止MouseUp事件继续传递。
二、处理挂起的路由事件
在本例中可启用按钮被挂起的MouseUp事件,操作方法是在按钮的Click事件中使用AddHandler()方法。该方法提供了一个重载版本,该版本可以接收一个Boolean值作为它的第三个参数。如果将该参数设置为true,那么即使设置了Handled标志,也将接收到事件。代码如下:
private void cmdClear_Click(object sender, RoutedEventArgs e)
{
//eventCounter = 0;
//lstMessages.Items.Clear();
cmdClear.AddHandler(UIElement.MouseUpEvent, new MouseButtonEventHandler(SomethingClicked), true);
}
此时单击按钮效果如下:
通常这种方式是不可取的,如果为按钮错误的处理了MouseUp事件,而没有处理Click事件,那么事件处理代码就只能对鼠标单击做出响应,而不能对相应的键盘操作做出响应。
三、代码下载
WpfApplication5_2.rar提取码:k98j