tags:

views:

755

answers:

2

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"/>

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 );
    }
}

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.

A: 

There are plenty of good examples out there covering attached events.

kek444
I had been looking at that very example, and I understand it, but I'm not sure why very similar code in my project isn't working. Even if I copy/paste from that article, XAML intellisense doesn't pick it up. Curious.
Josh Santangelo
The intellisense doesnt pick it up in any version of vs i know of, it's normal. Does the event work at all?
serialseb
A: 

In order to get the attached event to show up in Intellisense, it has to be in a class that resides in a satellite assembly -- or .dll library. The easiest way to add a library is add a "WPF Custom Control Library" project to your solution. Using a Wpf control library just ensures that all the typical References will be added automatically (which they won't with a C# class library.) You can delete the CustomControl1.cs just as long as you delete its associated Style in Themes/Generic.xaml.

countzero1984