public FastEntryViewModel(FastEntry model) { m_Model = model; m_Weights = new ObservableCollection <FloatViewModel>(); foreach (float weight in model.Weights) { FloatViewModel vm = new FloatViewModel(weight); vm.PropertyChanged += OnWeightValueChanged; m_Weights.Add(vm); } // Better ways of doing this, but yolo m_Weights.CollectionChanged += (sender, args) => { m_Model.Weights = m_Weights.Select(f => f.Value).ToList(); OnPropertyChanged(nameof(StartWeight)); OnPropertyChanged(nameof(EndWeight)); }; RefreshWeights(); RefreshDaysFasted(); }
private void RefreshWeights() { int numOfWeightSlots = 1; if (EndTime >= StartTime) { numOfWeightSlots = (int)(EndTime - StartTime).TotalDays + 1; } else { numOfWeightSlots = (int)(DateTime.Now - StartTime).TotalDays + 1; } ObservableCollection <FloatViewModel> newWeights = new ObservableCollection <FloatViewModel>(); float lastValue = float.NaN; for (int i = 0; i < numOfWeightSlots; ++i) { if (m_Weights.Count != 0 && i < m_Weights.Count) { lastValue = m_Weights[i].Value; newWeights.Add(m_Weights[i]); } else { FloatViewModel vm = new FloatViewModel(lastValue); vm.PropertyChanged += OnWeightValueChanged; newWeights.Add(vm); } } for (int i = numOfWeightSlots; i < m_Weights.Count; ++i) { m_Weights[i].PropertyChanged -= OnWeightValueChanged; } Weights = newWeights; }