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

时间:2018-09-23 20:14:06

标签: c# wpf routed-events

我有以下路由事件:

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

我将其触发(此代码确实被调用,我设置了一个断点):

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

我有一个使用该事件的XAML页面:

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

事件处理程序(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;
    }
}

现在,即使引发了事件,也永远不会调用事件处理程序-这为什么呢?如何获取自定义路由事件的事件处理程序?

1 个答案:

答案 0 :(得分:1)

在我要点击的for (DataSnapshot ing : postSnapshot.child("foods").getChildren()) { String name = ing.child("productName").getValue(String.class); String quantity = ing.child("quantity").getValue(String.class); String productId = ing.child("productId").getValue(String.class); AllOrders order = new AllOrders(productId, name, quantity); myfoods.add(order); } 上方,我还有BindableGrid叠加层;我必须在叠加层上设置BindableGrid,这样点击才能“通过”到我想单击的网格。

相关问题