#68 – Nesting XAML Elements Expresses Containment

Nesting elements is a fundamental part of XML syntax.  In XAML, nested elements typically expresses containment:

the .NET object corresponding to the child element is contained in the .NET object corresponding to the parent element.

Here’s an example.  The XAML fragment below defines a StackPanel control that contains two Button controls.

<StackPanel Height="100" Name="stackPanel1" Width="200">
    <Button Content="Button" Height="23" Name="button1" Width="75" />
    <Button Content="Button" Height="23" Name="button2" Width="75" />

The structure of the XAML elements (Button as child of StackPanel) mirrors the structure of the actual controls in the user interface (the buttons are children of the StackPanel).