<Window x:Class="DlgTreeView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Diálogo TreeView" Height="300" Width="400">
  <Window.Resources>
    <!-- Proveedor de datos XML -->
    <XmlDataProvider x:Key="xmlAlumnos" Source="datos/alumnos.xml"/>
    <!-- PLANTILLA ASIGNATURA -->
    <DataTemplate x:Key="kAsignatura">
      <WrapPanel>
        <TextBlock Text="{Binding XPath=nombre}" Padding="2" />
        <TextBlock Text=":   " />
        <TextBox Text="{Binding XPath=nota}" Padding="2" />
      </WrapPanel>
    </DataTemplate>
    <!-- PLANTILLA ASIGNATURAS -->
    <HierarchicalDataTemplate x:Key="kAsignaturas"
                              ItemsSource="{Binding XPath=asignatura}"
                              ItemTemplate="{StaticResource kAsignatura}">
      <TextBlock Text="{Binding XPath=@tipo}" Padding="2" />
    </HierarchicalDataTemplate>
    <!-- PLANTILLA ALUMNO -->
    <HierarchicalDataTemplate x:Key="kAlumno"
                              ItemsSource="{Binding XPath=asignaturas}"
                              ItemTemplate="{StaticResource kAsignaturas}">
      <TextBlock Text="{Binding XPath=nombre}" Padding="2" />
    </HierarchicalDataTemplate>
    <!-- PLANTILLA ALUMNOS -->
    <HierarchicalDataTemplate x:Key="kAlumnos"
                              ItemsSource="{Binding XPath=alumno}"
                              ItemTemplate="{StaticResource kAlumno}">
      <TextBlock Text="Notas de los alumnos" Padding="2" />
    </HierarchicalDataTemplate>
    <!-- Estilos para facilitar la expansión y contracción de los nodos -->
    <Style x:Key="Expandir" TargetType="{x:Type TreeView}">
      <Style.Resources>
        <Style TargetType="TreeViewItem">
          <Setter Property="IsExpanded" Value="True" />
        </Style>
      </Style.Resources>
    </Style>
    <Style x:Key="Contraer" TargetType="{x:Type TreeView}">
      <Style.Resources>
        <Style TargetType="TreeViewItem">
          <Setter Property="IsExpanded" Value="False" />
        </Style>
      </Style.Resources>
    </Style>
  </Window.Resources>
  <Grid DataContext="{Binding Source={StaticResource xmlAlumnos}, XPath=alumnos}">
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TreeView  Name="trvAlumnos" Margin="5"
               ItemsSource="{Binding}"
               ItemTemplate="{StaticResource kAlumnos}">
    </TreeView>
    <StackPanel Grid.Column="1">
      <Button Name="btGuardarDocXml" Content="Guardar doc xml" VerticalAlignment="Top" Margin="5" Click="btGuardarDocXml_Click" />
      <Button Name="btExpandirNodos" Content="Expandir nodos" VerticalAlignment="Top" Margin="5" Click="btExpandirNodos_Click" />
      <Button Name="btContraerNodos" Content="Contraer nodos" VerticalAlignment="Top" Margin="5" Click="btContraerNodos_Click" />
      <Button Name="btRecargarDocXml" Content="Recargar doc xml" VerticalAlignment="Top" Margin="5" Click="btRecargarDocXml_Click" />
    </StackPanel>
  </Grid>
</Window>
