#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);
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: