Reputation: 209
Cant seem to see where I am going wrong? the OnPropertyChange is not being recondnised any suggestions?
public class MedicationList : INotifyPropertyChanged
{
public int MedicationID { get; set; }
public string Description
{
get
{
return Description;
}
set
{
OnPropertyChanged( "Description" );
Description = value;
}
}
}
}
EDIT I have added public class MedicationList : INotifyPropertyChanged
Upvotes: 6
Views: 10688
Reputation: 552
You need to inherent BaseViewModel.
public class MedicationList : BaseViewModel
Upvotes: 0
Reputation: 62002
There's a difference between a base class and an interface.
With a base class, the members are automatically inherited and one needs to do nothing (except if some members need an override
). With an interface, the class does not inherit the interface members automatically; you have to introduce them in your class. If you don't the compiler will complain.
INotifyPropertyChanged
is an interface.
Upvotes: 0
Reputation: 236278
You should implement INotifyPropertyChanged interface, which has single PropertyChanged
event declared. You should raise this event if some of object's properties changed. Correct implementation:
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));
}
}
Upvotes: 7
Reputation: 910
You need the actual code the interface implements inside of your class.
/// <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));
}
}
Upvotes: 1
Reputation: 755239
This method needs to be defined by your type to raise the INotifyPropertyChanged::PropertyChanged
event
public PropertyChangedEventHandler PropertyChanged;
...
private void OnPropertyChanged(string propertyName) {
var saved = PropertyChanged;
if (saved != null) {
var e = new PropertyChangedEventArgs(propertyName);
saved(this, e);
}
}
Upvotes: 0
Reputation: 22084
I bet you want to do something like this:
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;
}
Upvotes: 3