RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


WPF Wonders: Properties and Events : Page 4

Setting properties and handling events in Windows Presentation Foundation are fundamentally different than they were in Windows Forms. Find out how to deal with properties and events in both XAML and WPF-targeted code.

Attached Events
The last topic in this article deals with attached events. The routed event is a WPF concept. When an action occurs, tunneling and bubbling events start rushing up and down the control hierarchy between the root element and the control that caused the event.

Sometimes it might be handy to intercept an event at some arbitrary point within the sequence of events. In code, this is easy. You simply place an event handler on the control where you want to catch the event. For example, if you want to catch a Button's Click event in the Grid that contains the Button, you can make the Grid catch the PreviewMouseLeftButtonUp event. Using XAML, you use an attached event to catch an event in a control other than the one that started the process.

The syntax for an attached event is similar to the syntax for an attached property. First, find the control that should catch the event, and give it an attribute named after the control that will raise the event, followed by a dot, followed by the event name.

For example, the sample program ButtonAttachedEvent uses the following code to make a StackPanel that holds two Buttons. The StackPanel's attribute Button.Click="SP_ButtonClick" indicates that the StackPanel should capture any Button.Click events raised inside it and send them to the SP_ButtonClick event handler. Each button also has a Click attribute that indicates the event handler that should catch that button's Click event.

   <StackPanel x:Name="LayoutRoot" Button.Click="Grid_ButtonClick">
       <Button Name="Button1" Content="Button 1"
        Margin="5" Height="50" Width="100"
        HorizontalAlignment="Center" VerticalAlignment="Center"
        Click="Button1_Click" />
       <Button Name="Button2" Content="Button 2"
        Margin="5" Height="50" Width="100"
        HorizontalAlignment="Center" VerticalAlignment="Center"
        Click="Button2_Click" />
If you run this program and click a Button, that Button's event handler catches the event. After that event handler finishes, the Grid_ButtonClick routine catches the event.

Attached events are probably most useful when you want one central event handler to catch events raised from several locations (such as having several buttons trigger the same action), but as this example shows, more than one event handler can catch the same event at different places in the control hierarchy.

Change Your Mind
WPF's dependency properties and routed events require big changes to the way you think about programming. In XAML, you can use property attribute syntax to set simple property values. Type converters translate the textual values (for example, "Red") into the appropriate data type (a Color in this case).

To set a property to a more complex value (such as a radial gradient brush), you can use property element syntax. After the control's start tag, create an element that defines the property's value.

Routed events make it possible to handle events wherever you like along the path between the control hierarchy's root and the object that started an event sequence. That has some immediate advantages, such as allowing a Button to fire its Click event even if you click on a control inside the Button instead of the button itself. It also allows you to catch and handle events in a more centralized global location by using attached events.

WPF properties and routed events are complicated, so you should expect to spend a little time to really get to know them. Until then, however, you can use Visual Studio and Expression Blend to set most property values and wire up event handlers. The information in this article will help you fill in gaps (for example, Visual Studio can make solid backgrounds but won't create gradient backgrounds for you) and help clarify the XAML that those tools build for you.

Rod Stephens is a consultant and author who has written more than a dozen books and two hundred magazine articles, mostly about Visual Basic. During his career he has worked on an eclectic assortment of applications for repair dispatch, fuel tax tracking, professional football training, wastewater treatment, geographic mapping, and ticket sales. His VB Helper web site receives more than 7 million hits per month and provides three newsletters and thousands of tips, tricks, and examples for Visual Basic programmers.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date