10

I have a data grid loading row event

_gridObj.LoadingRow += new EventHandler<DataGridRowEventArgs>(_gridObj_LoadingRow);

and in the handler I am creating another event. In the following code how can I know if the MouseLeftBtn event already exists for that row?

void _gridObj_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseLeftButtonUp += new MouseButtonEventHandler(Row_MouseLeftButtonUp);
}

Thanks,

Voodoo

VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • Do you want to test that an event handler is attached so you don't attach more than one event? i.e the LoadingRow event might get fired more than once per row? – aqwert Sep 27 '10 at 01:11
  • Yes, in the `_gridObj_LoadingRow` the `MouseLeftButtonUp` event is attached multiple times. I want to check whether the `MouseLeftButtonUp` event is already attached so I don't register another event for it. It is firing the `MouseLeftButtonUp` multiple times in my case here. – VoodooChild Sep 27 '10 at 03:26
  • Ok, I have added an answer which should meet your requirements. – aqwert Sep 27 '10 at 04:39

1 Answers1

15

Based on your comment that you don't want to attach muliple handlers in this case I unsubscribe then resubscribe. It does not give an error unsubscribing if none exists and ensures only one handler.

void _gridObj_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseLeftButtonUp -= new MouseButtonEventHandler(Row_MouseLeftButtonUp);
    e.Row.MouseLeftButtonUp += new MouseButtonEventHandler(Row_MouseLeftButtonUp);
}
aqwert
  • 10,559
  • 2
  • 41
  • 61