C# - INotifyPropertyChanged

Descripción general

Un pequeño apunte sobre como se implementa la interfaz INotifyPropertyChanged

#region "Patrón de implementación de [INotifyPropertyChanged]"
 /*
  * -----------------------------------------------------------
  * Mas información en:
  * http://stackoverflow.com/questions/4461865/
         pattern-for-implementing-inotifypropertychanged?rq=1
  * -----------------------------------------------------------
  */
 
 //Declarar el evento
 public event PropertyChangedEventHandler PropertyChanged;

 //Function OnEvento
 private void OnNotifyPropertyChanged(String nombrePropiedad)
 {
     if (String.IsNullOrEmpty(nombrePropiedad))
         throw new ArgumentException( 
          "nombre del método es null o empty.", "nombrePropiedad");
 
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(nombrePropiedad));
     }
 }
 #endregion

 private string myNombreDelEspectaculo;
 public string NombreDelEspectaculo
 {
     get { return this.myNombreDelEspectaculo; }
     set
     {
         if (string.IsNullOrWhiteSpace(value) == true)
         {
             throw new ArgumentException(
                 "Especifica un espectáculo",
                 "NombreDelEspectaculo");
         }
         /*INotifyPropertyChanged*/
         if (value != this.mNombreDelEspectaculo)
         {
             this.myNombreDelEspectaculo = value;
             OnNotifyPropertyChanged("NombreDelEspectaculo");
             // Alternativa
             // Obtener el nombre de la propiedad mediante reflexion
             // OnNotifyPropertyChanged(System.Reflection.MethodBase.GetCurrentMethod().Name);

         }
     }
 }
 

↑↑↑

A.2.Enlaces

[Para saber mas]
[Grupo de documentos]
[Documento Index]
[Bloque de apuntes tácticos de C#]
[Documento Start]
[Imprimir el Documento]