Class MainWindow 
  Private objTextBox As TextBox = Nothing
  Private objGrados As New CGrados()

  Private Sub formPrincipal_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
    ctGradosC.Focus()
    gridVentPpal.DataContext = objGrados
  End Sub

  Private Sub btAceptar_Click(sender As Object, e As RoutedEventArgs)
    If objTextBox Is Nothing Then Return
    Dim be As BindingExpression =
      objTextBox.GetBindingExpression(TextBox.TextProperty)
    be.UpdateSource()
    If be.HasError Then Return

    Try
      Dim grados As Double
      ' Si se escribió en la caja de texto grados centígrados...
      If objTextBox Is ctGradosC Then
        grados = Convert.ToDouble(ctGradosC.Text) * 9.0 / 5.0 + 32.0
        ' Mostrar el resultado redondeado a dos decimales
        ctGradosF.Text = String.Format("{0:F2}", grados)
        ctGradosF.GetBindingExpression(
          TextBox.TextProperty).UpdateSource()
      End If
      ' Si se escribió en la caja de texto grados Fahrenheit...
      If objTextBox Is ctGradosF Then
        grados = (Convert.ToDouble(ctGradosF.Text) - 32.0) * 5.0 / 9.0
        ' Mostrar el resultado redondeado a dos decimales
        ctGradosC.Text = String.Format("{0:F2}", grados)
        ctGradosC.GetBindingExpression(
          TextBox.TextProperty).UpdateSource()
      End If
      EstablecerMascara(Convert.ToDouble(ctGradosC.Text))
    Catch exc As FormatException
      ctGradosC.Text = "0,00"
      ctGradosF.Text = "32,00"
    End Try
  End Sub

  Private Sub ctGradosC_PreviewTextInput(sender As System.Object, e As System.Windows.Input.TextCompositionEventArgs)
    objTextBox = TryCast(sender, TextBox)
  End Sub

  Private Sub ctGradosF_PreviewTextInput(sender As System.Object, e As System.Windows.Input.TextCompositionEventArgs)
    objTextBox = TryCast(sender, TextBox)
  End Sub

  Private Sub ctGrados_GotKeyboardFocus(sender As System.Object, e As System.Windows.Input.KeyboardFocusChangedEventArgs)
    Dim ObjTextBox As TextBox = TryCast(sender, TextBox)
    ObjTextBox.SelectAll()
  End Sub

  Private Sub btSiguiente_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    objGrados.GradosC += 1
    objGrados.GradosF = objGrados.GradosC * 9.0 / 5.0 + 32.0
    MessageBox.Show(String.Format("{0:F2} ºC --> {1:F2} ºF", objGrados.GradosC, objGrados.GradosF))
  End Sub

  Private Sub EstablecerMascara(gradosC As Double)
    Dim skin As String = ""
    If objGrados.GradosC < 10 Then
      skin = "Skins/azul.xaml"
    ElseIf objGrados.GradosC > 30 Then
      skin = "Skins/amarillo.xaml"
    Else
      skin = "Skins/verde.xaml"
    End If

    Dim rd As New ResourceDictionary()
    rd.Source = New Uri(skin, UriKind.Relative)
    ' Cambiar el último diccionario de recursos
    Dim n As Integer = Application.Current.Resources.MergedDictionaries.Count
    Application.Current.Resources.MergedDictionaries(n - 1) = rd
  End Sub
End Class
