C# WPF路由事件触发,但未由处理程序处理
我有以下事件:C# WPF路由事件触发,但未由处理程序处理,c#,wpf,routed-events,C#,Wpf,Routed Events,我有以下事件: public static readonly RoutedEvent ItemMouseDownEvent = EventManager.RegisterRoutedEvent( "ItemMouseDown", RoutingStrategy.Bubble, typeof(ItemMouseDownEventHandler), typeof(BindableGrid)); public delegate void ItemMouseDownEventHandler(o
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); }
}
我这样启动它(这段代码确实会被调用,我设置了一个断点):
我有一个使用该事件的XAML页面:
<local:BindableGrid x:Name="starSystemMap" ArraySource="{Binding SpaceObjectArray}" CellSize="64" BackgroundImage="/Images/Starfield.png" ItemMouseDown="starSystemMap_ItemMouseDown">
...
</local:BindableGrid>
现在,即使事件被引发,事件处理程序也不会被调用-为什么会这样?如何为自定义路由事件调用事件处理程序?我在试图单击的
BindableGrid
上有额外的BindableGrid
覆盖层;我必须在覆盖层上设置ishitestvisible=“False”
,以便单击将“通过”到我想要单击的网格。该事件,ItemMouseDown
,如果注册一个处理程序,如void h(objects s,EventArgs e)=>MsgBox.Show(s)代码>我怀疑不是。也许您的事件声明不太正确。事件是用event
关键字声明的,这个关键字很特殊;我将把事件声明添加到我的帖子中。添加和删除处理程序是否运行?这很奇怪;我在Add/RemoveHandler行上设置了一个断点,但它们没有被执行。我声明事件处理程序的XAML是否有问题?找到答案!=>需要在覆盖上设置IshitteVisible=“False”。
<local:BindableGrid x:Name="starSystemMap" ArraySource="{Binding SpaceObjectArray}" CellSize="64" BackgroundImage="/Images/Starfield.png" ItemMouseDown="starSystemMap_ItemMouseDown">
...
</local:BindableGrid>
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;
}
}