ekolis
ekolis

Reputation: 6766

WPF routed event firing but not being handled by handler

I have the following routed event:

public static readonly RoutedEvent ItemMouseDownEvent = EventManager.RegisterRoutedEvent(
    "ItemMouseDown", RoutingStrategy.Bubble, typeof(ItemMouseDownEventHandler), typeof(BindableGrid));

public delegate void ItemMouseDownEventHandler(object sender, ItemMouseDownEventArgs e);

public class ItemMouseDownEventArgs : RoutedEventArgs
{
    public object Item { get; set; }
}

public event ItemMouseDownEventHandler ItemMouseDown
{
    add { AddHandler(ItemMouseDownEvent, value); }
    remove { RemoveHandler(ItemMouseDownEvent, value); }
}

And I'm firing it like so (this code does get called, I set a breakpoint):

var args = new ItemMouseDownEventArgs
{
    Item = ((FrameworkElement)sender).DataContext,
    RoutedEvent = ItemMouseDownEvent
};
RaiseEvent(args);

I have a XAML page consuming the event:

<local:BindableGrid x:Name="starSystemMap" ArraySource="{Binding SpaceObjectArray}" CellSize="64" BackgroundImage="/Images/Starfield.png" ItemMouseDown="starSystemMap_ItemMouseDown">
...
</local:BindableGrid>

And the event handler (WIP):

private void starSystemMap_ItemMouseDown(object sender, BindableGrid.ItemMouseDownEventArgs e)
{
    switch (e.Item)
    {
        case null:
            MessageBox.Show("Space... the final frontier... is very, very empty...");
            break;
    }
}

Now even though the event is being raised, the event handler never gets called - why is this? How can I get the event handler to be called for my custom routed event?

Upvotes: 1

Views: 515

Answers (1)

ekolis
ekolis

Reputation: 6766

I had additional BindableGrid overlays on top of the BindableGrid I was trying to click; I had to set IsHitTestVisible="False" on the overlays so the click would go "through" to the grid I wanted to click.

Upvotes: 1

Related Questions