Partial Public Class Formularios
    Inherits Page

    Public Sub New()
        InitializeComponent()
    End Sub

    'Se ejecuta cuando el usuario navega a esta página.
    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

    End Sub

  Private Sub guardarCambios_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    guardarCambios.IsEnabled = False
    ddsAlumnos.SubmitChanges()
  End Sub

  Private Sub ddsAlumnos_SubmittedChanges(sender As System.Object, e As System.Windows.Controls.SubmittedChangesEventArgs)
    If e.HasError Then
      Dim window As New ErrorWindow("Error:",
        String.Format(
          "Error, los cambios no fueron salvados: {0}",
          e.[Error].Message))
      window.Show()
      e.MarkErrorAsHandled()
    End If
    guardarCambios.IsEnabled = True
  End Sub

  Private Sub agregarAlumno_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim ventana As New VentanaNuevoAlumno()
    AddHandler ventana.Closed, AddressOf ventana_Closed
    ventana.Show()
  End Sub

  Private Sub ventana_Closed(sender As Object, e As EventArgs)
    Dim ventana As VentanaNuevoAlumno = TryCast(sender, VentanaNuevoAlumno)
    If ventana.Nuevo_alumno IsNot Nothing Then
      Dim alum As New alumno()
      alum.id_alumno = ventana.Nuevo_alumno.Id
      alum.nombre = ventana.Nuevo_alumno.NomAlumno
      Dim contextoDom As NotasAlumsDomSvc = TryCast(ddsAlumnos.DomainContext, NotasAlumsDomSvc)
      contextoDom.alumnos.Add(alum)
      ddsAlumnos.SubmitChanges()
    End If
  End Sub

  Private Sub borrarAlumno_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim contextoDom As NotasAlumsDomSvc = TryCast(ddsAlumnos.DomainContext, NotasAlumsDomSvc)
    contextoDom.alumnos.Remove(TryCast(dgAlumnos.SelectedItem, alumno))
    ddsAlumnos.SubmitChanges()
  End Sub
End Class
