tags:

views:

43

answers:

1

Hello

I have a SubMenu in a ContextMenu which ItemSource is set to a expression like

ContextMenu.Items[i].ItemsSource = DatabaseInstance.GetAllObjects()

When i handle the clicks from the ContextMenu i have this event handler: 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
        }

But even though the OriginalSource was created by an object of the type MyObject i always get is as a null.

How would i do this?

A: 

You can get the MenuItem instance in the handler and check the DataContext

if(((FrameworkElement)sender).DataContext is MyObject)
{
     // The clicked item is a MyObject, send to another method
}
Jobi Joy
Thank You. Though i had to use e.OriginalSource instead of sender
Erik