Graphics
If no element or set of elements provides you with the look you
want in your application, you can build it up from the set of graphics
primitives that WPF provides, including rectangles, polygons, lines, ellipses,
etc. WPF also lets you affect the way it renders graphics in any element,
offering facilities that include bordering, rotating, or scaling another shape
or control. WPF's support for graphics is engineered to fit right into the
content model we're already familiar with, as shown in
Example 1-38. from
Chapter 7.
Example 1-38. Adding graphics to a Button
<Button LayoutTransform="scale 3 3">
<StackPanel Orientation="Horizontal">
<Canvas Width="20" Height="18" VerticalAlignment="Center">
<Ellipse Canvas.Left="1" Canvas.Top="1" Width="16" Height="16"
Fill="Yellow" Stroke="Black" />
<Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3"
Fill="Black" />
<Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3"
Fill="Black" />
<Path Data="M 5,10 A 3,3 0 0 0 13,10" Stroke="Black" />
</Canvas>
<TextBlock VerticalAlignment="Center">Click!</TextBlock>
</StackPanel>
</Button>
Here we've got three ellipses and a path composed inside a
canvas, which is hosted inside a stack panel with a text block that, when
scaled via the LayoutTransform property on the button, produces
Figure 1-21.
Notice that there's nothing special about the graphic primitives
in XAML; they're declared and integrated as content just like any of the other
WPF elements we've discussed. The graphics and the
transformation are integrated into the same presentation stack as the rest of
WPF, which is a bit of a difference for User/GDI programmers of old.
Further, graphics in WPF are not limited to 2-D;
Figure 1-22 shows an example of a simple 3-D figure that was defined
declaratively just like a 2-D graphic.
For a complete discussion of how graphics primitives, retained
drawings, color, lines, brushes, and transformations happen in WPF, both
declaratively and in code, as well as an introduction to 3-D and video, you'll
want to read Chapter 7.
|