我在ContextMenu中有一个SubMenu,其中ItemSource设置为类似
的表达式ContextMenu.Items[i].ItemsSource = DatabaseInstance.GetAllObjects()
当我处理来自ContextMenu的点击时,我有这个事件处理程序: XALM:
<ContextMenu MenuItem.Click="ContextMenu_Click">
C#:
if (e.OriginalSource as MyObject == null) {
//Not MyObject. Choose action by comparing Header
}
else {
// The clicked item is a MyObject, send to another method
}
但即使OriginalSource是由MyObject类型的对象创建的,我总是得到它。
我该怎么做?
答案 0 :(得分:0)
您可以在处理程序中获取MenuItem实例并检查DataContext
if(((FrameworkElement)sender).DataContext is MyObject)
{
// The clicked item is a MyObject, send to another method
}