csharp
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new CustomButton();
}
private void Handler_ConditionalClick(object sender, MyRoutedEventArgs e)
{
MessageBox.Show(e.RoutedMessage);
}
}
public class MyRoutedEventArgs : RoutedEventArgs
{
public string RoutedMessage { get; set; }
public MyRoutedEventArgs(RoutedEvent routedevent) : base(routedevent){ }
}
public class CustomButton : Button
{
//具有浮升
//策略的路由事件的标识符命名为 <event name>Event。 例如,如果事件名为 Tap,则标识符应命名为 TapEvent。
//具有
//隧道策略的路由事件的标识符命名为 Preview<event name>Event。 例如,如果事件名为 Tap,则标识符应命名为 PreviewTapEvent。
public static readonly RoutedEvent ConditionalClickEvent = EventManager.RegisterRoutedEvent(
name: "ConditionalClick",
routingStrategy: RoutingStrategy.Bubble,
handlerType: typeof(EventHandler<MyRoutedEventArgs>),
ownerType: typeof(CustomButton));
// Provide CLR accessors for assigning an event handler. .NET CLR 属性包装
public event RoutedEventHandler ConditionalClick
{
add { AddHandler(ConditionalClickEvent, value); }
remove { RemoveHandler(ConditionalClickEvent, value); }
}
protected override void OnClick()
{
MyRoutedEventArgs routedEventArgs = new MyRoutedEventArgs(routedevent: ConditionalClickEvent) { RoutedMessage="zen"};
RaiseEvent(routedEventArgs);
base.OnClick();
}
}
自带事件应用
1》》
2》》