简体   繁体   English

WPF 路由事件触发但未被处理程序处理

[英]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:我有一个使用该事件的 XAML 页面:

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

And the event handler (WIP):和事件处理程序(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?如何为我的自定义路由事件调用事件处理程序?

I had additional BindableGrid overlays on top of the BindableGrid I was trying to click;我在尝试单击的BindableGrid顶部有额外的BindableGrid覆盖; I had to set IsHitTestVisible="False" on the overlays so the click would go "through" to the grid I wanted to click.我必须在叠加层上设置IsHitTestVisible="False"以便点击会“通过”到我想要点击的网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM