//激发路由时间,借助Click事件的激发方法 protected override void OnClick( ) { //保证Button原有的功能正常使用,Click事件能被激发 base.OnClick( ); ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this); args.ClickTime = DateTime.Now; this.RaiseEvent(args); }
//ReportTimeEvent路由事件处理器 private void ReportTimeHandler(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); if (element == this.grid_2)//至此事件不必往下传递了 { e.Handled = true; } }