Class MainWindow
  Public Property FicheroGuardar() As DestinoDatos

  'Public Shared ReadOnly FicheroGuardarProperty As DependencyProperty =
  '  DependencyProperty.Register("FicheroGuardar",
  '                              GetType(DestinoDatos),
  '                              GetType(MainWindow),
  '                              New UIPropertyMetadata(Nothing))

  'Public Property FicheroGuardar() As DestinoDatos
  '  Get
  '    Return DirectCast(GetValue(FicheroGuardarProperty), DestinoDatos)
  '  End Get
  '  Set(value As DestinoDatos)
  '    SetValue(FicheroGuardarProperty, value)
  '  End Set
  'End Property

  Sub New()
    ' Llamada necesaria para el diseñador.
    InitializeComponent()
    Me.FicheroGuardar = New DestinoDatos()
    Me.DataContext = FicheroGuardar
  End Sub

#Region "Órdenes enrutadas"

  Public Shared RCSalir As New RoutedUICommand()
  Public Shared RCAbrir As New RoutedUICommand()

  Private Sub ExecutedRCAbrir(sender As Object, e As ExecutedRoutedEventArgs)
    ' Implemente aquí la lógica de la orden RCAbrir
    MessageBox.Show("Orden Archivo > Abrir... personalizada ejecutada")
  End Sub

  Private Sub CanExecuteRCAbrir(sender As Object, e As CanExecuteRoutedEventArgs)
    ' Retorna True sólo si el destino de la orden es un control
    Dim destino As Control = TryCast(e.Source, Control)

    If destino IsNot Nothing Then
      e.CanExecute = True
    Else
      e.CanExecute = False
    End If
  End Sub

  ' Guardar
  Private Sub ExecutedGuardar(sender As Object, e As ExecutedRoutedEventArgs)
    ' Implemente aquí la lógica de la orden Guardar
    MessageBox.Show("Se guarda el texto """ +
                    DirectCast(e.Source, TextBox).Text & """ en " +
                    e.Parameter.ToString())
  End Sub

  Private Sub CanExecuteGuardar(sender As Object, e As CanExecuteRoutedEventArgs)
    ' Retorna True sólo si el destino de la orden es un TextBox
    If TypeOf e.Source Is TextBox Then
      e.CanExecute = True
    Else
      e.CanExecute = False
    End If
  End Sub
#End Region

#Region "Controladores de eventos"

  Private Sub ArchivoSalir_Click(sender As Object, e As RoutedEventArgs)
    ' Cerrar la ventana principal
    Close()
  End Sub

  Private Sub ArchivoAbrir_Click(sender As Object, e As RoutedEventArgs)
    System.Diagnostics.Debug.WriteLine("Archivo > Abrir...")
    MessageBox.Show("Archivo > Abrir")
  End Sub

  Private Sub ArchivoGuardarMismoNombre_Click(sender As Object, e As RoutedEventArgs)
    System.Diagnostics.Debug.WriteLine("Archivo > Guardar > Mismo nombre")
  End Sub

  Private Sub ArchivoGuardarOtroNombre_Click(sender As Object, e As RoutedEventArgs)
    System.Diagnostics.Debug.WriteLine("Archivo > Guardar > Otro nombre")
  End Sub

  Private Sub AyudaAcercaDe_Click(sender As Object, e As RoutedEventArgs)
    System.Diagnostics.Debug.WriteLine("Ayuda > Acerca de...")
  End Sub

  Private Sub textBox1_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs)
    FicheroGuardar.Destino = "datos1.txt"
  End Sub

  Private Sub textBox2_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs)
    FicheroGuardar.Destino = "datos2.txt"
  End Sub

  Private Sub ArchivoSalir_MouseEnter(sender As System.Object, e As System.Windows.Input.MouseEventArgs)
    Me.btexbarestPpal.Text = "Cierra la aplicación"
  End Sub

  Private Sub ArchivoSalir_MouseLeave(sender As System.Object, e As System.Windows.Input.MouseEventArgs)
    Me.btexbarestPpal.Text = "Listo"
  End Sub

#End Region
End Class
