(一)使用WPF内置路由事件

xaml:

 <Window x:Class="WpfApplication1.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         Title="Simple Binding" Height="200" Width="200">
     <Grid x:Name="gridRoot" Background="Lime" ButtonBase.Click="ButtonClicked">
         <Grid x:Name="gridA" Margin="10" Background="Blue">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition/>
                 <ColumnDefinition/>
             </Grid.ColumnDefinitions>
             <Canvas x:Name="canvasLeft" Grid.Column="0" Background="Red" Margin="10">
                 <Button x:Name="buttonLeft" Content="Left" Width="40" Height="100" Margin="10"/>
             </Canvas>
             <Canvas x:Name="canvasRight" Grid.Column="1" Background="Yellow" Margin="10">
                 <Button x:Name="buttonRight" Content="Right" Width="40" Height="100" Margin="10"/>
             </Canvas>
         </Grid>
     </Grid>
 </Window>

xaml.cs:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 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;
 using System.Data;
 using MySql.Data;
 using MySql.Data.Entity;
 using MySql.Data.MySqlClient;
 using System.Xml;
 using System.Xml.Linq;
 using System.IO;

 namespace WpfApplication1
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
             //this.gridRoot.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.ButtonClicked));
         }
         private void ButtonClicked(object sender, RoutedEventArgs e)
         {
             MessageBox.Show((e.OriginalSource as FrameworkElement).Name);
         }
     }
 }

这里的时间从底层冒泡上去到gridRoot处侦听到该时间,才MessageBox.Show出来

(二)自定义路由事件

书上的例子有问题,不能通过

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Data;
 using System.Windows.Controls;

 namespace WpfApplication1
 {
     class TimeButton : Button
     {
         public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent
             ("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));

         public event RoutedEventHandler ReportTime
         {
             add { this.AddHandler(ReportTimeEvent, value); }
             remove { this.RemoveHandler(ReportTimeEvent, value); }
         }

         protected override void OnClick()
         {
             base.OnClick();
             ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);
             args.ClickTime = DateTime.Now;
             this.RaiseEvent(args);
         }

     }
 }
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;

 namespace WpfApplication1
 {
     class ReportTimeEventArgs : RoutedEventArgs
     {
         public ReportTimeEventArgs(RoutedEvent routedEvent, object source)
             : base(routedEvent, source) { }
         public DateTime ClickTime { get; set; }
     }
 }
 <Window x:Class="WpfApplication1.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         Title="Simple Binding" x:Name="window_1" Height="300" Width="300"
         local:TimeButton.ReportTime="ReportTimeHandler">
     <Grid x:Name="grid_1" local:TimeButton.ReportTime="ReportTimeHandler">
         <Grid x:Name="grid_2" local:TimeButton.ReportTime="ReportTimeHandler">
             <Grid x:Name="grid_3" local:TimeButton.ReportTime="ReportTimeHandler">
                 <StackPanel x:Name="stackPanel_1"
                         local:TimeButton.ReportTime="ReportTimeHandler">
                     <ListBox x:Name="listBox"/>
                     <local:TimeButton x:Name="timeButton" Width="80" Height="80"
                                       Content="报时" local:TimeButton.ReportTime="ReportTimeHandler"/>
                 </StackPanel>
             </Grid>
         </Grid>
     </Grid>
 </Window>
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 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;
 using System.Data;
 using MySql.Data;
 using MySql.Data.Entity;
 using MySql.Data.MySqlClient;
 using System.Xml;
 using System.Xml.Linq;
 using System.IO;

 namespace WpfApplication1
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
             //this.gridRoot.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.ButtonClicked));
         }
         private void ReportTimeHander(object sender, ReportTimeEventArgs e)
         {
             FrameworkElement element = sender as FrameworkElement;
             string timeStr = e.ClickTime.ToLongTimeString();
             string content = string.Format("{0} 到达 {1}", timeStr, element.Name);
             this.listBox.Items.Add(content);
         }
     }
 }

.NET: WPF 路由事件的更多相关文章

  1. WPF自学入门(三)WPF路由事件之内置路由事件

    有没有想过在.NET中已经有了事件机制,为什么在WPF中不直接使用.NET事件要加入路由事件来取代事件呢?最直观的原因就是典型的WPF应用程序使用很多元素关联和组合起来,是否还记得在WPF自学入门(一 ...

  2. WPF路由事件二:路由事件的三种策略

    一.什么是路由事件 路由事件是一种可以针对元素树中的多个侦听器而不是仅仅针对引发该事件的对象调用处理程序的事件.路由事件是一个CLR事件. 路由事件与一般事件的区别在于:路由事件是一种用于元素树的事件 ...

  3. WPF 路由事件 Event Routing

    原文:WPF 路由事件 Event Routing 1.路由事件介绍 之前介绍了WPF的新的依赖属性系统,本篇将介绍更高级的路由事件,替换了之前的.net普通事件.相比.net的事件,路由事件具有更强 ...

  4. WPF 路由事件总结

    1.什么是路由事件 已下为MSDN中的定义 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 CLR 事件,可以由 R ...

  5. WPF自学入门(四)WPF路由事件之自定义路由事件

    在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...

  6. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  7. WPF路由事件

    ​    这节讲一下WPF中的路由事件(Routed Event). [什么是事件] 在了解路由事件前,我们应先来了解一下什么是事件(Event). 在Windows系统中,像鼠标单击,双击,移动这样 ...

  8. WPF路由事件学习(一)

    路由事件与一般事件的区别在于:路由事件是一种用于元素树的事件,当路由事件触发后,它可以向上或向下遍历可视树和逻辑树,他用一种简单而持久的方式在每个元素上触发,而不需要任何定制的代码(如果用传统的方式实 ...

  9. WPF 路由事件

    最近想封装一个关于手势的控件,但是由其他的控件覆盖之后发现不能触发,据说是有一些事件在定义的时候就处理过e.Handle了. 定义的时候就处理了,就是为了控件能够正常的工作,别如Button.Mous ...

随机推荐

  1. 解决微软的两个恶心问题(VS2008死机、Win2008 WAS无法启动)

    1.Visual Studio 2008,在切换到Web设计界面或Html Markup界面时,过一段时间就出现假死,点击任何地方没反应,也关闭不了,只能用任务管理器结束任务. 上网查了下,应该是先装 ...

  2. BLE 4.0 与 4.1的区别

    蓝牙技术让我们在连接各种设备的时候不再被繁多的数据线所束缚,比如音响.电脑,甚至是汽车.目前最新的蓝牙版本是4.0,相比3.0它进一步降低了功耗,并且也提高了传输效率.近日,蓝牙技术联盟(Blueto ...

  3. 业界有很多MQ产品

    目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...

  4. Java迭代 : Iterator和Iterable接口

    从英文意思去理解 Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... Iterator:   在英语中or 结尾是都是表示 .. ...

  5. C++控制程序只运行一个实例

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ...

  6. 公共POI导出Excel方法--java

    最早开始的时候做过一些数据Excel导出的功能,但是到后期每一次导出都需要写一些差不多类似的代码,稍微研究了一下写了个公共的导出方法. 这里用的是POI,然后写成了一个公共类,传入设置好格式的数据,就 ...

  7. Windows Runtime - 面向对象化的C++(并非意味着托管)

    Windows 8的开发平台总体上分为两部分:一是全新的WinRT,界面搭配Metro style,二是传统的Win32..NET(SL).IE三大平台,界面为传统窗体风格.其中全新的WinRT被微软 ...

  8. Dynamics AX Read OLEDB

    static System.Data.DataTable getOLEDB_Record(str _dbPath,str _query) { System.Data.OleDb.OleDbConnec ...

  9. 10 Ways to Inspire Your Team

    Inspire. Just the word itself causes us to pause and think. We may remember our own personal heroes ...

  10. Java学习-027-JSON 之一 -- 初识

    通常,我们在生活中使用的应用产品(无论是 Web应用还是 App 应用),又或者我们平常的信息交流,均无可避免的存在着信息交互,在信息交互的过程中就有着信息载体的存在.那么,在我们熟知的网络应用产品中 ...