0

I have an observable collection property that holds some instances of a custom class and a string property that outputs the sum of some data in the observable collection property. Like this...

public ObservableCollection<MyClass> MyClasses
{
    get
    {
        return _myClasses;
    }
    set
    {
        _myClasses = value;
        OnPropertyChanged("MyClasses");
        // I thought the following might work but it doesn't seem to
        OnPropertyChanged("TotalTime");
    }
}

public string TotalTime
{
    get
    {
        int totalTime = 0;

        foreach (MyClass myClass in MyClasses)
        {
            totalTime += myClass.Timespan;
        }

        return TimeSpan.FromMinutes(totalTime).ToString(@"h\:mm\:ss");
    }
}

As elements are added/removed from MyClasses I have a TextBlock that I want to bind the Text property to TotalTime, but it is not working.

Any ideas or tricks to make this work?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • What have you tried? Provide the information. Binding to `TotalTime` seems like that should work. – ΩmegaMan Nov 15 '18 at 23:26
  • 1
    The setter will not be invoked when you add/remove items. You could use the collection changed event to publish the property changed event for TotalTime. – musium Nov 15 '18 at 23:38
  • Possible duplicate of [ObservableCollection setter isn't firing when item is added](https://stackoverflow.com/questions/17183812/observablecollection-setter-isnt-firing-when-item-is-added) – EpicKip Nov 16 '18 at 14:17
  • @musium - What about for when an item in the collection is changed? Like a string property in an item is updated? –  Nov 16 '18 at 15:19

0 Answers0