Reputation: 1505
Apologies if the title is misleading, but I'm not sure precisely what to call what I'm looking for here. I have the following "main" class:
public class Entity : INotifyPropertyChanged
{
public string Name { get; set; }
public EntityRole Role { get; set; }
public EntityStats Stats = new EntityStats();
//Other stuff....
}
And a ... sub class? (proper name would be appreciated for this) ... called EntityStats
:
public class EntityStats : INotifyPropertyChanged
{
public int CurrentHealth
{
get { return _currentHealth; }
set
{
if (value != _currentHealth)
{
_currentHealth = value;
OnPropertyChanged("CurrentHealth");
}
}
}
//other properties...
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
Where an Entity
object contains a property of type EntityStats
.
On the other side of my code, I'm registering an Entity
object to listen for PropertyChanged
events:
public void RegisterEntity(Entity entity)
{
entity.Stats.PropertyChanged += entity_PropertyChanged;
}
void entity_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "CurrentHealth")
{
Message.Write("CurrentHealth property changed!");
DeathCheck((Entity)sender);
}
}
The issue I'm having is with DeathCheck((Entity)sender);
-- Because the CurrentHealth
OnPropertyChanged
event is part of the EntityStats
class, the object is of type EntityStats
, which only contains part of the data I need.
How can I determine the Entity
object that sender
belongs to, or how can I refactor this code so that when a property inside EntityStats
changes, an event is raised in the Entity
class?
Upvotes: 1
Views: 239
Reputation: 1045
You can listen the EntityStats event's in Entity constructor and dispatch a Entity event's. Like this:
public class Entity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Entity() {
Stats.PropertyChanged += Stats_PropertyChanged
}
//Other stuff....
void Stats_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
e.PropertyName = "Stats." + e.PropertyName;
PropertyChanged(this, e);
}
}
}
Now, in the RegisterEntity, the listener method can be associated direct to the Entity instance:
public void RegisterEntity(Entity entity)
{
entity.PropertyChanged += entity_PropertyChanged;
}
and the sender of the listener method is Entity instance, but is possible identify if the property was changed in Stats property:
void entity_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Stats.CurrentHealth")
{
Message.Write("Stats.CurrentHealth property changed!");
DeathCheck((Entity)sender);
}
}
Upvotes: 4
Reputation: 8551
You could give the EntityStats
class a property named Entity
(or somesuch) and set that property in the EntityStats
constructor:
public class Entity : INotifyPropertyChanged
{
public string Name { get; set; }
public EntityRole Role { get; set; }
public EntityStats Stats = new EntityStats(this);
//Other stuff....
}
public class EntityStats : INotifyPropertyChanged
{
private Entity _entity;
public Entity Entity
{
get { return _entity; }
}
public EntityStats(Entity entity)
{
_entity = entity;
}
// ...
}
Upvotes: 1