Imports System.ComponentModel

Public Delegate Sub ItemEditEventHandler(sender As IEditableObject)

Class CTelefonoBO
  Implements INotifyPropertyChanged, IEditableObject

  Private _nombre As String
  Public Property Nombre() As String
    Get
      Return _nombre
    End Get
    Set(value As String)
      Validar(value)
      _nombre = value
      OnPropertyChanged(New PropertyChangedEventArgs("Nombre"))
    End Set
  End Property

  Private _direccion As String
  Public Property Direccion() As String
    Get
      Return _direccion
    End Get
    Set(value As String)
      Validar(value)
      _direccion = value
      OnPropertyChanged(New PropertyChangedEventArgs("Direccion"))
    End Set
  End Property

  Private _telefono As String
  Public Property Telefono() As String
    Get
      Return _telefono
    End Get
    Set(value As String)
      Validar(value)
      _telefono = value
      OnPropertyChanged(New PropertyChangedEventArgs("Telefono"))
    End Set
  End Property

  Private _observaciones As String
  Public Property Observaciones() As String
    Get
      Return _observaciones
    End Get
    Set(value As String)
      _observaciones = value
      OnPropertyChanged(New PropertyChangedEventArgs("Observaciones"))
    End Set
  End Property

  Public Sub New()
  End Sub
  Public Sub New(nom As String, dir As String, tel As String, Optional obs As String = Nothing)
    Nombre = nom
    Direccion = dir
    Telefono = tel
    Observaciones = obs
  End Sub

  Private _modificado As Boolean
  Public Property Modificado() As Boolean
    Get
      Return _modificado
    End Get
    Set(value As Boolean)
      _modificado = value
    End Set
  End Property

  Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged
  Public Sub OnPropertyChanged(e As PropertyChangedEventArgs)
    _modificado = True
    RaiseEvent PropertyChanged(Me, e) ' generar evento
  End Sub

  ' Eventos ItemBeginEdit e ItemEndEdit
  Public Event ItemBeginEdit As ItemEditEventHandler
  Public Event ItemEndEdit As ItemEditEventHandler

  ' Copia para cuando se cancele la operación de edición
  Private copia As CTelefonoBO
  Private enEdicion As Boolean = False

  Public Sub BeginEdit() Implements IEditableObject.BeginEdit
    If enEdicion Then Return
    enEdicion = True
    copia = TryCast(Me.MemberwiseClone(), CTelefonoBO)
    RaiseEvent ItemBeginEdit(Me) ' generar evento
  End Sub

  Public Sub CancelEdit() Implements IEditableObject.CancelEdit
    If Not enEdicion Then Return
    enEdicion = False
    Me.Nombre = copia.Nombre
    Me.Direccion = copia.Direccion
    Me.Telefono = copia.Telefono
    Me.Observaciones = copia.Observaciones
    Me.Modificado = copia.Modificado
  End Sub

  Public Sub EndEdit() Implements IEditableObject.EndEdit
    If Not enEdicion Then Return
    enEdicion = False
    copia = Nothing
    RaiseEvent ItemEndEdit(Me) ' generar evento
  End Sub

  ' Validación a nivel de celda
  Private Sub Validar(dato As String)
    If dato.Length = 0 Then
      Throw New ArgumentException("Esta columna no se puede dejar vacía")
    End If
  End Sub
End Class
