#70 – Specifying Event Handlers in XAML

In addition to setting property values, you can also wire up event handlers in XAML.  You do this by specifying the name of the event and the name of the method in your code-behind that should be called when the event fires.

Here’s an example:

<Button Name="btnClickMe" Content="Click Me" Height="23" Width="75" Click="Button_Click"/>

This wires up this button’s Click event to the Button_Click method in the containing window’s code-behind (.cs) file.

private void Button_Click(object sender, RoutedEventArgs e)
{
}

Adding the event handler in XAML is equivalent to doing it in code as follows:

this.btnClickMe.Click += new RoutedEventHandler(Button_Click);
Advertisement