2013-07-26 14 views
5

Non riesco a vedere dove sbaglio? OnPropertyChange non sta ricondizionando alcun suggerimento?OnPropertyChange non esiste nel contesto corrente?

public class MedicationList : INotifyPropertyChanged 
{ 



    public int MedicationID { get; set; } 

    public string Description 
    { 
     get 
     { 
      return Description; 
     } 
     set 
     { 
      OnPropertyChanged("Description"); 
      Description = value; 

     } 
    } 
} 

}

EDIT Ho aggiunto public class MedicationList : INotifyPropertyChanged

+1

Hai dichiarato un metodo 'OnPropertyChanged' in qualsiasi punto della classe' MedicationList'? Non lo vedo –

+5

Ricorsione rilevata: 'Descrizione = valore;' –

+0

La classe deve implementare l'interfaccia INotifyPropertyChanged –

risposta

6

Si dovrebbe implementare INotifyPropertyChanged interfaccia, che dispone di camere singole PropertyChanged evento dichiarato. Dovresti sollevare questo evento se alcune proprietà dell'oggetto sono cambiate. Corretta attuazione:

public class MedicationList : INotifyPropertyChanged 
{ 
    private string _description; // storage for property value 

    public event PropertyChangedEventHandler PropertyChanged; 

    public string Description 
    { 
     get { return _description; } 
     set 
     { 
      if (_description == value) // check if value changed 
       return; // do nothing if value same 

      _description = value; // change value 
      OnPropertyChanged("Description"); // pass changed property name 
     } 
    } 

    // this method raises PropertyChanged event 
    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) // if there is any subscribers 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

Penso che la parte importante dell'OP sia che 'OnPropertyChanged' non appare magicamente dal nulla - nemmeno aggiungendo' INotifyPropertyChanged' alla lista delle interfacce implementate. –

+0

Il nome onPropertychange non esiste nel contesto corrente – Mark

+0

@Mark effettivamente esiste, nella parte inferiore della definizione di classe –

2

scommetto che si vuole fare qualcosa di simile:

public class MedicationList : INotifyPropertyChanged { 
    public int MedicationID { get; set; } 
    private string m_Description; 

    public string Description { 
    get { return m_Description; } 
    set { 
     m_Description = value; 
     OnPropertyChanged("Description"); 
    } 
    } 

    private void OnPropertyChanged(string propertyName) { 
    if (string.IsNullOrEmpty(propertyName)) 
     throw new ArgumentNullException("propertyName"); 

    var changed = PropertyChanged; 
    if (changed != null) { 
     changed(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 
+4

Ricorsione rilevata anche qui. :) Descrizione = valore – Nitesh

+0

@Nitesh - buona cattura, risolto :) –

0

Questo metodo deve essere definita dal vostro tipo per generare l'evento INotifyPropertyChanged::PropertyChanged

public PropertyChangedEventHandler PropertyChanged; 

... 

private void OnPropertyChanged(string propertyName) { 
    var saved = PropertyChanged; 
    if (saved != null) { 
    var e = new PropertyChangedEventArgs(propertyName); 
    saved(this, e); 
    } 
} 
1

È necessario il codice effettivo che l'interfaccia implementa all'interno della tua classe.

/// <summary> 
/// Public event for notifying the view when a property changes. 
/// </summary> 
public event PropertyChangedEventHandler PropertyChanged; 

/// <summary> 
/// Raises the PropertyChanged event for the supplied property. 
/// </summary> 
/// <param name="name">The property name.</param> 
internal void OnPropertyChanged(string name) 
{ 
    PropertyChangedEventHandler handler = PropertyChanged; 
    if (handler != null) 
    { 
     handler(this, new PropertyChangedEventArgs(name)); 
    } 
} 
0

C'è una differenza tra una classe base e un'interfaccia.

Con una classe base, i membri vengono ereditati automaticamente e non è necessario fare nulla (tranne se alcuni membri hanno bisogno di un override). Con un'interfaccia, la classe non eredita automaticamente i membri dell'interfaccia; devi presentarli nella tua classe. Se non lo fai il compilatore si lamenterà.

INotifyPropertyChanged è un'interfaccia.

Problemi correlati