If the method is virtual, the user can either increase the basic functionality by calling the base class method, or replace the functionality of the base class without calling the base class method. For OnEvent () methods, if you do not call the base class method, this event will not increase (this is the responsibility of the base class method.) If the base class does some kind of state management inside the OnEvent method, this means that the derived class may accidentally invalidate the state object, if the user decided to omit the method call of the base class. The documentation may indicate "always call the base class method", but there is no way to enforce it.
When I see an event that does not have a virtual OnEvent () method, I usually assume that this method performs some kind of internal state control, and the class developers want to guarantee their state control starts. This is not the case in FrameworkElement, and this is not the only event that does not match the pattern, so I'm curious what reasoning is.
I turned around in Reflector to find out if I can find the reason. There is an OnDataContextChanged () method, but it is a handler for changing dependency properties and does not conform to standard event patterns. This is probably the reason that it is not protected by virtual. This is non-standard, so this will be confusing. This is static, so you still cannot override it. Since it is automatically called by the structure of dependency properties, and you cannot override it, I believe that we have a reason why it is private and not static virtual.
You can use another template to display the normal event template:
class FrameworkElement {
My guess is why they did not? Usually you call OnEvent () to raise an event. The event automatically occurs when the DataContext changes, and you do not need to raise it at any other time.
Owenp source share