#607 – An Instance Handler vs. A Class Handler

In WPF, when you define an event handler, the event handler is typically an instance handler–i.e. an event handler associated with a specific instance of a control.  In the XAML below, we’ve defined an event handler for one particular Button.

<Button Content="Like" Click="btnLike_Click"/>

You can also define a class handler–an event handler that is associated with all instances of a particular type, rather than one instance.  It will be called whenever the event in question passes through any instance of the specified type.

For example, we can define a handler that will be called when the user clicks on any Button control.

        public MainWindow()

            // Call when user clicks on any Button
                new RoutedEventHandler(HandleAllButtons));

        private void HandleAllButtons(object sender, RoutedEventArgs e)
            Button b = (Button)e.Source;
            Trace.WriteLine(string.Format("* You clicked on [{0}] button", b.Content));

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #607 – An Instance Handler vs. A Class Handler

  1. Pingback: Dew Drop – July 20, 2012 (#1,368) | Alvin Ashcraft's Morning Dew

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

%d bloggers like this: