Check if an event already exists

跟風遠走 提交于 2019-12-23 08:05:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!