Should a derived class handle base class events? (C # / WPF)

I am creating a class that comes from a WPF RichTextBox control, and I need to execute some code in copy and paste events.

I understand that, when possible, it is best to implement event-based code in a derived class by overriding the base class method that raises the event. However, in this case, such a method does not exist, and is it acceptable for my derived class to add an event handler to its own base class events?

If I add an event handler, I assume that it should be explicitly deleted when the control is removed. However, I'm not sure how best to do this in the case of the RichTextBox , as the WPF management classes do not seem to have any mechanism for detecting deletion.

Any suggestions please?

Thanks Tim

+4
source share
1 answer

Of course, you can handle base class events. This is usually done for the Loaded event, for example, since the OnLoaded method OnLoaded missing.

You do not need to worry about removing the handler: since the publisher and event subscriber are the same instance, not deleting the handler will not prevent GC from collecting your object.

+4
source

All Articles