I am creating an MVVM application in which a class ToBeListenedhas a couple of properties, PropertyAand PropertyB, and I want to listen to them.
public class ToBeListened : INotifyPropertyChanged
{
private double _propertyA;
private string _propertyB;
public double PropertyA{get; set; }
public double PropertyB{get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
These two properties are listened to by the class Listener, so I injected an EventHandler into it that listens for the object ToBeListened.
public class Listener
{
private ToBeListened toBeListenedObject;
public Listener()
{
toBeListenedObject = new ToBeListened();
toBeListenedObject.PropertyChanged += newPropertyChangedEventHandler(PropertyListener_PropertyChanged);
}
private void PropertyListener_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch(e.PropertyName)
{
case "PropertyA":
{
}
case "PropertyB":
{
}
}
The fact is that I do not really like the solution I found. A is switch-casenot compatible with polymorphism, therefore
- Is there a better way to do this? Maybe something that uses overload ? (How
private void PropertyListener_PropertyChanged(double sender, PropertyChangedEventArgs e) - First of all, is it correct to encode ViewModel as follows?
source
share