Reputation: 7195
I have a class
public class SomeModel : INotifyPropertyChanged {
public ObservableCollection<SomeSubModel> SubModels {get; set;}
public int Sum { get { return SubModels.Sum(x=> x.Count) }}
private string _url;
public string Url
{
get { return _url; }
set
{
_url = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class SomeSubModel : INotifyPropertyChanged {
private string _count;
public string Count
{
get { return _count; }
set
{
_count = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
I'm going to use binding to SomeSubModel.Sum
in WPF UI.
The SomeSubModel.Count
property is changed very frequently.
How to notify that SomeSubModel.Sum
is changed when property SomeSubModel.Count
from any item in SomeModel.SubModels
observable collection is changed to reflect the actual SomeSubModel.Sum
in WPF UI through the binding?
The main goal is to reflect in UI the actual Sum of all Count's of objects in observable collection.
Thank you!
Upvotes: 0
Views: 516
Reputation: 28737
You should fire a notify property changed for the Sum property as well in that case:
private string _count;
public string Count
{
get { return _count; }
set
{
_count = value;
OnPropertyChanged();
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs("Sum"));
}
}
Upvotes: 2