'OnNotifyPropertyChanged only triggers on the firts obj of the List
I have a ParentObj" Country " which has a List child Obj "Cities". I coded a OnNotifyPropertyChanged
in the child object that notifies the Parent about its changes.
If I change the first obj of the child list it works! But if I change the second one, it does not trigger the event.
public abstract class AbstractInvestor : IInvestor, INotifyPropertyChanged
{
private string name;
private Stock stock;
public abstract event PropertyChangedEventHandler PropertyChanged;
protected int wealth;
public abstract int Wealth //this is child property
{
get;
set;
}
protected abstract void OnNotifyPropertyChanged(string propertyName);
public AbstractInvestor(string name)
{
this.name = name;
}
public void Update(Stock stock)
{
Console.WriteLine("Notified {0} of {1}'s " +
"change to {2:C}", name, stock.Symbol, stock.Price);
}
// Gets or sets the stock
public Stock Stock
{
get { return stock; }
set { stock = value; }
}
}
public class Investor: AbstractInvestor
{
public override event PropertyChangedEventHandler PropertyChanged;
public Investor(string name) : base(name)
{
}
public override int Investment //this is child property
{
get { return investment; }
set
{
if (investment!= value)
{
OnNotifyPropertyChanged(value.ToString());
wealth = value;
}
}
}
protected override void OnNotifyPropertyChanged(string propertyName)
{
var tmp = PropertyChanged;
if (tmp != null)
{
tmp(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public abstract class Stock
{
private string symbol;
private double price;
public List<IInvestor> investors = new List<IInvestor>();
public int MarketCap { get; set; }
public Stock(string symbol, double price)
{
this.symbol = symbol;
this.price = price;
}
private void MarketCapChanged(object o, PropertyChangedEventArgs e)
{
int valore = int.Parse(e.PropertyName);
if (valore!= MarketCap)
{
this.MarketCap += valore;
Console.WriteLine($"Maket value now is:{this.MarketCap}");
}
}
public void Attach(IInvestor investor)
{
var ivs = (Investor) investor;
ivs.PropertyChanged += MarketCapChanged;
investors.Add(ivs);
}
public void Detach(IInvestor investor)
{
investors.Remove(investor);
}
public void Notify()
{
foreach (IInvestor investor in investors)
{
investor.Update(this);
}
Console.WriteLine("");
}
// Gets or sets the price
public double Price
{
get { return price; }
set
{
if (price != value)
{
price = value;
Notify();
}
}
}
// Gets the symbol
public string Symbol
{
get { return symbol; }
}
}
static void Main(string[] args)
{
IBM ibm = new IBM("IBM", 120.00);
ibm.Attach(new Investor("Sorros"));
ibm.Attach(new Investor("Berkshire"));
ibm.investors[0].Wealth = 1000;
ibm.investors[1].Wealth = 1000;
//// Fluctuating prices will notify investors
//ibm.Price = 120.10;
//ibm.Price = 121.00;
//ibm.Price = 120.50;
//ibm.Price = 120.75;
// Wait for user
Console.ReadKey();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|