自定义路由事件
分三步:
①声明并注册路由事件
public static readonly RoutedEvent ButtonClickEvent = EventManager.RegisterRoutedEvent
("ButtonClick",RoutingStrategy.Bubble,typeof(RoutedEventArgs),typeof(自定义类));
②定义路由事件包装(封装)
,点击搜索,即调用ButtonClick
public event RoutedEventHandler ButtonClick
{
add{ this.AddHandler(ButtonClickEvent , value); }
remove{ this.removeHandler(ButtonClickEvent , value);}
}
③触发路由事件
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ControlTemplate ct = this.Template;
Label lbl = ct.FindName("lblLeftMouseDown", this) as Label;
lbl.MouseLeftButtonDown += lbl_MouseLeftButtonDown;
}
void lbl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
RoutedEventArgs routedEventArgs = new RoutedEventArgs(WaterTextBoxSearchControl.ButtonClickEvent, sender);
this.RaiseEvent(routedEventArgs);//点击label的时候触发这个事件,可以在前台调用这个事件
}