路由事件的处理模型常用的有两种:
冒泡事件: 由子控件位次向父容器传递,大部分的路由事件都是冒泡事件
隧道事件: 由父容器位次向其子容器、控件传递,一般PreXXX事件属性隧道事件
下面提供键盘事件和鼠标事件
键盘的隧道事件
<Window x:Class="Event.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="KetEvent" Height="350" Width="480" WindowStartupLocation="CenterScreen" Focusable="True" PreviewKeyDown="Window_PreviewKeyDown"> <Canvas> <!--..--> <TextBox x:Name="txtMessage" Margin="5" Width="300" Height="20" Canvas.Top="10" KeyDown="txtMessage_KeyDown"/> <TextBox x:Name="txtVolumeUp" Margin="5" Width="300" Height="20" Canvas.Top="40" KeyDown="txtVolumeUp_KeyDown"/> <TextBox x:Name="txtVolumeDown" Margin="5" Width="300" Height="20" Canvas.Top="70" KeyDown="txtVolumeDown_KeyDown"/> </Canvas> </Window>
后台
/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Up) { //按下"静音"键 txtMessage.Text = "Up"; e.Handled = true; } else if (e.Key == Key.Down) { //按下"增大音量"键 txtVolumeUp.Text = "Down"; e.Handled = true; } else if (e.Key == Key.Left) { //按下"减小音量"键 txtVolumeDown.Text = "Left"; e.Handled = true; } } private void txtMessage_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show("Message"); } private void txtVolumeUp_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show("VolumeUp"); } private void txtVolumeDown_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show("VolumeDown"); } }
在窗体上使用
PreviewKeyDown="Window_PreviewKeyDown"事件,事件前面使用PreView代表隧道事件下面。
KeyDown="txtMessage_KeyDown"事件,属于冒泡事件
鼠标的事件
<Window x:Class="MouseEvent.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Canvas PreviewMouseDown="Canvas_PreviewMouseDown"> <TextBox x:Name="txtMessage" Canvas.Left="50" Canvas.Top="46" Width="150" Height="20"/> <Rectangle Canvas.Left="246" Canvas.Top="46" Height="118" x:Name="mainRectangle" Stroke="Black" Width="200" Fill="White" MouseEnter="mainRectangle_MouseEnter" MouseLeave="mainRectangle_MouseLeave" MouseMove="mainRectangle_MouseMove" MouseDown="mainRectangle_MouseDown" MouseWheel="mainRectangle_MouseWheel"/> </Canvas> </Window>
后台代码
/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void mainRectangle_MouseEnter(object sender, MouseEventArgs e) { //鼠标进入控件时,控件的颜色为红色 mainRectangle.Fill = new SolidColorBrush(Colors.Red); } private void mainRectangle_MouseLeave(object sender, MouseEventArgs e) { //鼠标离开控件时,控件的颜色为红色 mainRectangle.Fill = new SolidColorBrush(Colors.White); } private void mainRectangle_MouseDown(object sender, MouseButtonEventArgs e) { //获取点击的鼠标的按钮 MouseButton button = e.ChangedButton; txtMessage.Text += " "; txtMessage.Text += string.Format("Mouse Button is {0}", button.ToString()); } private void mainRectangle_MouseMove(object sender, MouseEventArgs e) { //获取基于Rectangle的鼠标的坐标 Point pointBaseRectange = Mouse.GetPosition(mainRectangle); txtMessage.Text = string.Format("Mouse Position (Base the Rectangle) is ({0},{1})", pointBaseRectange.X, pointBaseRectange.Y); txtMessage.Text += " "; //获取基于窗体的鼠标的坐标 Point pointBaseWindow = Mouse.GetPosition(this); txtMessage.Text += string.Format("Mouse Position(Base thi Window) is ({0,1})", pointBaseWindow.X, pointBaseWindow.Y); } private void mainRectangle_MouseWheel(object sender, MouseWheelEventArgs e) { if (e.Delta > 0) { //如果向上推动滚轮,图形的宽度增加 mainRectangle.Width++; } if (e.Delta < 0) { //如果向下推动滚轮,图形的宽度减小 mainRectangle.Width--; } } }
在窗体上使用
PreviewMouseDown="Canvas_PreviewMouseDown"事件,事件前面使用PreView代表隧道事件下面。
MouseDown="mainRectangle_MouseDown"事件,属于冒泡事件