Charles Petzold



Where Have All the XAML Flowers Gone?

April 5, 2006
New York City

Wherever I go, people ask me "Charles, when are you going to write a XAML version of the famous Flower program that has been delighting generations of programmers since it first appeared in OS/2 Presentation Manager Programming (1994)?"

Flower.xaml is now here, and it's a "mini-language" extravaganza.

As some of you might know, XAML once had several "mini-languages" that allowed you to cut down some XAML flab with simple (but cryptic) text strings. The mini-languages for gradient brushes and transforms have been removed. (As many of us have suspected, it's because Microsoft has decided that XAML is for tools rather than hand-coding — a horrifying attitude that means to turn us all into drag-and-drop automatons and which I am totally ignoring in my book. I'm teaching XAML, not how to get repetitive stress injuries.)

The two mini-languages left are for attributes of type Geometry (documented in Path.Data) and Transform (documented I don't know where but which simply consists of the six variable cells of Matrix). I use both of them in my XAML flower program, listed here:

Graphics transforms are a bit odd in WPF. Mostly you apply transforms directly to elements (such as controls or graphical shapes). This causes them to translated, scaled, skewed, or rotated relative to their pre-transform selves. By default, rotation is relative not to the origin of the drawing surface, but to the origin of the element.

You can also apply a transform to a Canvas, as is done here. The 200-square unit Canvas is flipped horizontally and shifted to give me a four-quadrant Cartesian coordinate system with 100 units in all four directions. If you give the Canvas a non-default Background you'll see that it's rendered mostly outside the drawing area, but because it's been subjected to a RenderTransform rather than a LayoutTransform, everybody — and particularly the Viewbox — is under the mistaken impression that it still occupies its original location.