C# and WPF
So far, just about everything you’ve done with WPF has been declarative; that is, all the functionality has taken place in the XAML file. WPF is specifically designed that way, to be useful to designers as well as to developers. The only C# you’ve had to write so far has been some very rudimentary event handlers. In this section you’re going to create an example that more closely resembles a production application, and that’s going to involve a supporting class, and some event handlers.
In this example, you’re going to grab the images of the first 20 presidents of the United States from the White House’s website, and present them in a custom WPF control, a modified ListBox
control. The control will not be wide enough to show all 20 images, so you’ll provide a horizontal scroll bar, and as the user moves the mouse over an image, you’ll provide feedback by enlarging that image (from 75 to 85) and increasing its opacity from 75% to 100%. As the user moves the mouse off the image, you’ll return the image to its smaller, dimmer starting point.
This application will use some declarative animation, as you’ve already seen, although slightly subtler than the rotating square. In addition, when the user clicks on an image, you’ll handle the click and display the name of the president using a C# event handler, and then you’ll reach into the control and place the president’s name into the title bar of the control.
Figure 19-7 shows the result of scrolling to the 16th president and clicking ...
Get Learning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.