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 followingpublic 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 !