Overriding RoutedEventArgs.InvokeEventHandler Method

Overriding of the InvokeEventHandler method is a MUST when deriving custom RoutedEvent argument class from a descendant of RoutedEventArgs class!

Let’s we want to derive our custom ClickEventArgs class from MouseButtonEventArgs to pass MouseButton information and some additional information on custom routed ClickEvent.

It could look like follows:

/// <summary>
Click Event arguments.
/// </summary>
public class ClickEventArgs : MouseButtonEventArgs
    /// <summary>
Initializes a new instance of the <see cref="ClickEventArgs"/> class.
/// </summary>
    /// <param name="tag">
Some more event data.</param>
    /// <param name="args">
The <see cref="System.Windows.Input.MouseButtonEventArgs"/>
instance containing the base event data.</param>
public ClickEventArgs(object tag, MouseButtonEventArgs args)
        : base(args.MouseDevice, args.Timestamp, args.ChangedButton, args.StylusDevice)
        RoutedEvent = Item.ClickEvent;
        Tag = tag;

    /// <summary>
Gets or sets additional event data object.
/// </summary>
    /// <value>
Any object or null.</value>
public object Tag { get; private set; }

    /// <summary>
Invokes event handlers in a type-specific way, which can increase event system
/// efficiency.
/// </summary>
    /// <param name="genericHandler">
The generic handler to call in a type-specific way.</param>
    /// <param name="genericTarget">
The target to call the handler on.</param>
protected override void InvokeEventHandler(Delegate genericHandler, Object genericTarget)
        ((ClickEventHandler)genericHandler).Invoke(genericTarget, this);

If the override of InvokeEventHandler method is missed from the class above we’ll have InvalidCastException raising the Click event:

The reason is that the base MouseButtonEventArgs class in its own InvokeEventHandler method override attempts to cast genericHandler to MouseButtonEventHandler and, of course, fails.

Oleg V. Polikarpotchkin


About ovpwp

I am engaged in programming, maintenance and supply of computing technique since 1970. Started with the computers M, BESM, Minsk series and received appropriate education in the least, in which it was then possible in THE USSR. Then he went the usual way - ES series, IBM 360/370, Sun Spark, Ibm Power & PS2, PC. Programming started in the code (machine commands), then were sorts of assemblers, Algol, FORTRAN, PL/1, C, C , VB, C#. It is only the ones that I used the production scale; but there were, of course, others like List, Modula, Pascal, Java, etc. Currently I prefer .NET platform for desktop development. I don't really like web-programming (probably because of the inability for quality design), but I have enough experience in site building in LAMP environment using PHP.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s