private void WeightChanged(Object sender, PropertyChangedEventArgs e) { if (e.PropertyName != "Value" || _changing != null) { return; } // here's where we adjust the other values if one changes. var total = Weights.Sum(x => x.Value); var makeup = 100 - total; if (makeup == 0) { return; } var addToEach = makeup / (Weights.Count - 1); if (addToEach == 0) { return; } _changing = (WeightValueViewModel)sender; foreach (var wvm in Weights) { if (wvm == _changing) { continue; } wvm.Value += addToEach; if (wvm.Value < 0) { wvm.Value = 0; } if (wvm.Value > 100) { wvm.Value = 99; } } // handle rounding error, charge the one the user is modifying total = Weights.Sum(x => x.Value); if (total != 100) { _changing.Value += 100 - total; } _changing = null; }
public WeightViewModel(IList <IIndicator> indicators) { if (indicators == null) { Weights = new List <WeightValueViewModel>(); return; } Weights = indicators.Select(x => { var model = new WeightValueViewModel { Value = (Int32?)(x.Weight * 100), Indicator = x }; model.PropertyChanged += WeightChanged; return(model); }).ToList(); }