Search code examples
c#.netwpf

Getting all the attached event handlers for a WPF control


I am developing an application where I assign the events for the button dynamically. Now the thing is that I wish to get all the events for the button click event as I wish to remove the previous handlers.

I tried setting the event handler to null like:

Button.Click += null;

However I received a runtime exception that null cannot be assigned.

I wish to know the events which are already attached to it so that I can remove those events.

Can anybody help me in achieving this?


Solution

  • You cannot assign events - only attach (+=) and remove (-=) operations are available for clients.

    Since += and -= are the only operations that are permitted on an event outside the type that declares the event, external code can add and remove handlers for an event, but cannot in any other way obtain or modify the underlying list of event handlers.