检查事件已存在 [英] Check if an event already exists
本文介绍了检查事件已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据网格加载行事件
I have a data grid loading row event
_gridObj.LoadingRow += new EventHandler<DataGridRowEventArgs>(_gridObj_LoadingRow);
和在处理程序中,我创建另一个事件。在下面的代码我怎么能知道是否MouseLeftBtn事件已存在该行?
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,
巫毒
推荐答案
根据您的意见,你不希望在这种情况下,我再重新订阅退订附加muliple处理程序。它不给一个错误,如果不存在,并确保只有一个处理退订。
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);
}
这篇关于检查事件已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文