
Events and XAML Code-Behind
Most WPF applications consist of both markup and code-behind. Within a project, the XAML is written as a .xaml file, and a CLR language such as Microsoft Visual Basic .NET or C# is used to write a code-behind file. When a XAML file is compiled, the location of the XAML code-behind file for each XAML page is identified by specifying a namespace and class as the x:Class attribute of the root element of the XAML page.
In the examples so far, you have seen several buttons, but none of these buttons had any logical behavior associated with them yet. The primary application-level mechanism for adding a behavior for an object element is to use an existing event of the element class, and to write a specific handler for that event that is invoked when that event is raised at run time. The event name and the name of the handler to use are specified in the markup, whereas the code that implements your handler is defined in the code-behind.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyNamespace.MyPageCode">
<Button Click="ClickHandler" >Click Me!</Button>
</Page>
namespace MyNamespace
{
public partial class MyPageCode
{
void ClickHandler(object sender, RoutedEventArgs e)
{
Button b = e.Source as Button;
b.Background = Brushes.Red;
}
}
}
Notice that the code-behind file uses the CLR namespace MyNamespace and declares MyPageCode as a partial class within that namespace. This parallels the x:Class attribute value of MyNamespace.MyPageCode that was provided in the markup root. The compiler will automatically create a partial class for any compiled XAML page, by deriving a class from the root element type. When you provide code-behind that also defines the same partial class, the resulting code is combined within the same namespace and class of the compiled application.
For more information about requirements for code-behind programming, see the "Code-behind, Event Handler, and Partial Class Requirements" section of Code-Behind and XAML.
If you do not want to create a separate code-behind file, you can also inline your code within a XAML file. However, inline code is a less versatile technique that has substantial limitations. For details, see Code-Behind and XAML.
Event Attribute Syntax
When you specify behavior through events in markup, you typically use attribute syntax to attach handlers. The object element where the event attribute is specified becomes the instance that listens for the event and calls the handler. The name of the specific event you want to handle is the attribute name. The attribute value is the method name of the handler you will define. You must then provide the handler implementation in code-behind, with the handler being based on the delegate for that event. You write the handler in code-behind in a programming language such as Microsoft Visual Basic .NET or C#.
Each WPF event will report event data when the event is raised. Event handlers can access this event data. In the preceding example, the handler obtains the reported event source through the event data, and then sets properties on that source.
Routed Events
A particular event feature that is unique and fundamental to WPF is a routed event. Routed events enable an element to handle an event that was raised by a different element, as long as the elements are connected through an element tree relationship. When specifying event handling with a XAML attribute, the routed event can be listened for and handled on any element, including elements that do not list that particular event in the class members table. This is accomplished by qualifying the event name attribute with the owning class name. For instance, the parent StackPanel in the ongoing StackPanel / Button example could register a handler for the child element button's Click event by specifying the attribute Button.Click on the StackPanel object element, with your handler name as the attribute value. For more information, see Routed Events Overview.