Custom attached events in WPF

Jos*_*elo 5 c# wpf events routed-events attachedbehaviors

I might be getting the terminology wrong here, but I think I'm trying to create an attached event.

In the Surface SDK, you can do things like:

<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>
Run Code Online (Sandbox Code Playgroud)

I want to create a custom event for which a handler can be added in XAML in the same way, but I'm having trouble.

I can create a custom routed event, but the XAML intellisense doesn't see it and the event handler isn't added if I just type it in regularly. Here is my event definition:

public static class TagRectEvents
{
    public static readonly RoutedEvent TagRectEnterEvent = EventManager.RegisterRoutedEvent(
        "TagRectEnter", RoutingStrategy.Bubble, typeof( RoutedEventHandler ), typeof( TagRectEvents ) );

    public static void AddTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.AddHandler( TagRectEvents.TagRectEnterEvent, handler );
    }

    public static void RemoveTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.RemoveHandler( TagRectEvents.TagRectEnterEvent, handler );
    }
}
Run Code Online (Sandbox Code Playgroud)

Am I just going about it all wrong? All of the "attached behavior" examples I see are more about adding an attached property, and then doing things with elements that set that property.

cou*_*984 -1

为了使附加事件显示在 Intellisense 中,它必须位于驻留在卫星程序集(或 .dll 库)中的类中。添加库的最简单方法是将“WPF 自定义控件库”项目添加到您的解决方案中。使用 Wpf 控件库只是确保自动添加所有典型引用(使用 C# 类库则不会)。只要在 Themes/Generic 中删除其关联的 Style,就可以删除 CustomControl1.cs。 xaml。