问题
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
回答1:
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);
}
来源:https://stackoverflow.com/questions/3800412/check-if-an-event-already-exists