
454
LESSON 40 Making WPF aPPlications
<Grid>
<Image Height=”100” HorizontalAlignment=”Left” Margin=”12,12,0,0”
Name=”butterflyImage” Source=”/Critters;component/Butterfly.jpg”
Stretch=”Uniform” VerticalAlignment=”Top” Width=”100”
MouseDown=”butterflyImage_MouseDown” />
... Other Image controls omitted ...
</Grid>
</Window>
If you study this code, you’ll see that the Window contains a Grid and the Grid contains the Image
controls. You’ll also notice that the XAML code gives the name of the code-behind event handlers
that should execute when the user presses the mouse down on an
Image. In the code shown here, the
MouseDown event handler is called butterflyImage_MouseDown.
A Window can contain only a single child control. Normally that is a container
such as a
Grid that holds all of the other controls.
Although this simple application works, it could use some improvements. For example, the images
are not centered vertically. They also do not resize to take advantage of the form’s current size.
One of the philosophical goals of WPF is to make controls arrange themselves to make the best use
of whatever spaces is available. In a Windows Forms application you might use
Anchor properties to
do this. In a WPF application, you generally use container controls that help arrange their children.
The following section shows how you can improve the way this program arranges its controls.
ARRANGING ...