PETZOLD BOOK BLOG

Charles Petzold on writing books, reading books, and exercising the internal UTM


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

XAML Rules (but Code Suffers)

November 19, 2005
Roscoe, NY

Let's create a Grid, which in the Windows Presentation Foundation (formerly code-named Avalon) derives from Panel and lets you organize controls and other UI elements into a grid.

In XAML, the Grid probably begins with a bunch of ColumnDefinition and RowDefinition elements that might look something like this:

The first ColumnDefinition indicates the column should have a width of 100 pixels, the second means that the column is sized to its contents, and the third and fourth means that the leftover space is to be apportioned equally.

Now let's do it in code. The code uses the Add method of the ColumnDefinitions collection in conjunction with the GridUnitType enumeration and the GridLength class. The code is a little bulkier than the XAML but it's not bad:

Well, no. There actually is no overload of the Add method that lets you do it like this. Instead, you need to create each ColumnDefinition object explicitly and add it to the ColumnDefinitions collection. The code ends up looking something like this:

Which, of course, is just ridiculous.

Why is something that is so easy to do in XAML so clumsy to do in code? I can think of only two explanations:

Neither of these explanations is very comforting.


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold
www.charlespetzold.com