Pergunta

I have made a context menu which is activated via MouseDownevent. This event checks if the user clicked the right button and if so opens the menu. I am using the same event to open the same context menu for a listbox and a listview. Is there a way to check which one of them activated the MouseDown event?

Edit: I'll be a bit more specific. I can tell which controller activated the event from the event itself.. I want to know which controller activated the event from the context menu item which has been clicked on.

Foi útil?

Solução 2

I solved it by using the Tag property of the context menu. I put there the sender object which triggered the event, and then I could just do:

ListView lv = resultsContextMenu.Tag as ListView;
if (lv == null) //listbox was the one to call the mouse down event
{ //do stuff }

this code was called inside the menu items themselves that were chosen by the user

Outras dicas

If you have something like that:

private void MouseDown(object sender, MouseButtonEventArgs e)
{

}

you can check sender:

if(sender is ListView)
{
    //event fired by ListView
} 

if(sender is ListBox)
{
    //event fired by ListBox
} 

etc.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top