1:常规的鼠标路由事件定义:
#region 路由事件
public static readonly RoutedEvent CrossIconClickedEvent = EventManager.RegisterRoutedEvent("CrossIconClicked",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FilterLabel));
#endregion
#region CLR事件
public event RoutedEventHandler CrossIconClicked
{
add { this.AddHandler(CrossIconClickedEvent, value); }
remove { this.RemoveHandler(CrossIconClickedEvent, value); }
}
#endregion
#region 私有方法
/// <summary>
/// 点击删除图标,引发CrossIconClicked事件
/// </summary>
private void ImageDel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//引发路由事件
RoutedEventArgs newEvent = new RoutedEventArgs(FilterLabel.CrossIconClickedEvent, this);
this.RaiseEvent(newEvent);
}
#endregion
2:键盘路由事件定义
#region 路由事件
public static readonly RoutedEvent FreeKeyDownEvent = EventManager.RegisterRoutedEvent("FreeKeyDown",
RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(FreeDatePicker));
#endregion
#region CLR事件
//
// 摘要:
// 在焦点位于此元素上并且用户按下键时发生。
public event KeyEventHandler FreeKeyDown
{
add { this.AddHandler(FreeKeyDownEvent, value); }
remove { this.RemoveHandler(FreeKeyDownEvent, value); }
}
#endregion
/// <summary>
/// 触发FreeKeyDown事件(非鼠标的键盘路由事件)
/// </summary>
private void Date_TextBox_KeyDown(object sender, KeyEventArgs e)
{
KeyEventArgs newEvent = new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, e.Key);
newEvent.RoutedEvent = FreeDatePicker.FreeKeyDownEvent;
this.RaiseEvent(newEvent);
}
原文地址:https://www.cnblogs.com/changbaishan/p/9355900.html