Infosys Microsoft Alliance and Solutions blog

« Using User Control in Windows WPF (XAML) Application | Main | WPF - Animate with less code »

Adding Event handlers for controls in WPF (XAML) applications

I have been extremely used to the way event handlers are added to the code behind files, be it web application or winform application, when working with Visual Studio. This could be done either by double clicking the form/control whose event you want to handle or go the event listing in the properties windows, pick the event and double click there.

However the same doesn't works for WPF applications. It doesn't work using the currently available WPF extensions that work with VS2005 or with March CTP of Orcas.

This missing support seems like a big handicap and is like going backwards. Writing the correct event handler signature is important to get the event to work correctly and this becomes a challenge. One can look up the documentation, but that incurs a hit on productivity. So what's the way out.

I recently hit upon a simple technique by which we can get the right event handlers added in no time and I used another feature of VS for it.

Assuming you have already created a new Windows WPF application and added controls to the XAML file and now want to handle events for the controls. Take for example you have an Image control, and you want to handle the mouse left button down event for this image control. First add the necessary attribute in the XAML for the requried event. VS intellisense comes handy for this. The XAML statement will look something like the following

<Image Name="imgCtrl" Source="/Images/map.png" MouseLeftButtonDown="MouseLBtnClickHandler"></Image>

Where MouseLBtnClickHandler is name you want to use for method in the code behind file. Next go to the code behind file and in the already added public constructor for the Window, just below the InitializeComponent() method, add code to create a new Image control. Next, using Intellisense or otherwise, start writing code to add new event handler using the += (C# syntax) for the event you want to handle. VS will at this time, popup a tooltip that will help you add a new method to handle this event, with the appropriate signature. See the figure below.


Go ahead and allow VS to add this code snippet for you by hitting the TAB key twice. New method will be added with the appropriate signature. Rename the method to the same name as mentioned in the XAML file and also delete the lines added in the constructor (newing up the control and adding the event handler). Compile and run the code and you will see the event handler correctly wires up for the respective event. The final code will look something like the following

public Window1()

void MouseLBtnClickHandler(object sender, MouseButtonEventArgs e)
     //your mouse left button down handling code goes here

[Edited: 13 Nov 2007] After adding a control in the XAML, if you build the project once, the name for that control is visible in intellisence in the code behind. So you can do that while adding your event handlers. It will avoid you to create a dummy control just to add the event handler.

Comments welcome !


Thanks for the article. Stands out in the web as a place to start looking at WPF XAML for programmers.

Good article. You think MS will add the event wiring from the VS in the final release or do we have to do the way you described above to handle events.

Ramesh: I sure hope that the event wiring will get added. Haven't really seen any discussion around it though.

However considering that designers could be working on the XAML separately and devs on the code separately, we may have to live with this approach as well !!

There is an easier way.
I use the beta of Expression Blend.
Add your button. Click on events in the properties.
Then double click on the click event, blend adds to the xaml and code for you. Simple, and you can have blend and vs studio running at the same time.

Steve: Thanks for sharing. However I guess this is OK if I am already using Expression Blend for desinging the UI. However I don't think I will like to use it only for adding event handlers

Thank you. The post was very helpful.

ThankYou very much. Post was helpful.

I want to add a common event to some obj on wpf. But I can't. Can you help me?

Ali, can you elaborate a bit more on your need? A use a common event handler, just use the same method for all event handlers. Like multiple button's Click handler can be the same method.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter