December 2, 2003 .net

More Love for XAML Syntax: Dotted Element Names

Don Box simplified my XAML code using resources and styles. He also illustrates another XAML-ism that I’m coming to love: dotted element names. When you see something a dotted element name in XAML:

<GridPanel>
  <GridPanel.Resources>...</GridPanel.Resources>

  ...
</GridPanel>

what you’re looking at is a more convenient way of specifying an XML attribute using XML element syntax. For example, while you can specify a menu item like s

<MenuItem Header="New" />

However, if you’re doing anything fancy, like specifying an access key, you can do this using the dotted element name syntax:

<MenuItem>
  <MenuItem.Header>

    <FlowPanel>
      <AccessKey Key="N" />
      <SimpleText>ew</SimpleText>
    </FlowPanel>
  </MenuItem.Header>
</MenuItem>

In the first case, we’re just specifying a string, so declaring the Header attribute inline makes sense. In the second case, we’re composing the Header for the MenuItem as a FlowPanel, combining an AccessKey and a SimpleText element. Underneath the covers, XAML attributes translate into properties, so for those properties more complicated than strings, the dotted element name syntax allows properties to be specified as sub-elements.