<?xml version="1.0" encoding="utf-8" ?>
<Mcml xmlns="http://schemas.microsoft.com/2008/mcml" 
      xmlns:cor="assembly://MsCorLib/System"
      xmlns:coll="assembly://MsCorLib/System.Collections"
      xmlns:btn="<!--Path to Button.mcml-->"
      xmlns:code="<!--Path to the assembly containing the ListSelection class-->"
      xmlns:me="Me">
  
  <!--simple list of buttons, reports the selected item and its index-->
  <UI Name="List">

    <Locals>
      
      <!--passed to list items, holds the selected item and its index-->
      <code:ListSelection Name="Selection" />
      
    </Locals>
    
    <Properties>
      
      <!--data source to create the list items from-->
      <coll:IList Name="Model" IList="$Required" />
      
      <!--command invoked on the selected item when clicked-->
      <ICommand Name="Command" ICommand="$Required" />

      <!--selected item-->
      <cor:Object Name="SelectedItem" Object="null" />
      
      <!--index of the selected item-->
      <cor:Int32 Name="SelectedIndex" Int32="-1" />

    </Properties>

    <Rules>
      
      <!--bind selection to properties-->
      <Binding Source="[Selection.Index]" Target="[SelectedIndex]" />      
      <Binding Source="[Selection.Item]" Target="[SelectedItem]" />
      
    </Rules>
    
    <Content>
      
      <!--repeats the content for each item in the data source-->
      <Repeater Source="[Model]">
        <Layout>
          <GridLayout AllowWrap="true" Orientation="Vertical" />
        </Layout>
        <Content>
          <!--list item requires details about the current item and must be able to update the selection-->
          <me:ListItem Index="[RepeatedItemIndex]" Item="[RepeatedItem]" SelectionHolder="[Selection]">
            <Model>
              <!--invokes the main command while providing a unique description for each item-->
              <InvokeCommand Description="[RepeatedItem.ToString]" Target="[Command.Invoke]" />
            </Model>
          </me:ListItem>
        </Content>
      </Repeater>
      
    </Content>
    
  </UI>

  <!--UI element for each item in a list, extends the Button-->
  <UI Name="ListItem" BaseUI="btn:Button">

    <Properties>
      
      <!--reference to the list's selection-->
      <code:ListSelection Name="SelectionHolder" ListSelection="$Required" />
      
      <!--data source item (and its index) that was used to create the list item-->
      <Index Name="Index" Index="$Required" />
      <cor:Object Name="Item" Object="$Required" />
      
    </Properties>

    <Rules>
      
      <!--updates the list's selection when the list item receives keyboard/mouse focus-->
      <Condition Source="[Input.KeyFocus]" SourceValue="true">
        <Actions>
          <Set Target="[SelectionHolder.Index]" Value="[Index.Value]" />
          <Set Target="[SelectionHolder.Item]" Value="[Item]" />
        </Actions>
      </Condition>
      
    </Rules>
    
  </UI>
  
</Mcml>