Validation: ObservableCollection

Aug 15, 2011 at 7:57 AM
Edited Aug 15, 2011 at 8:16 AM


Following my previous post, I have run into another bump. Looks like I am having a little bit of difficulty in working with MVVM pattern. Hope you can help.

  • I have a property in my model: ObservableCollection<Subregion> Regions
  • Subregions have a property called "Percent"
  • Subregion also derives from ModelBase.
  • When you add the percent of subregions, that should be equal to 100
  • I have derived my model from ModelBase
  • I have implemented :
        private ObservableCollection<SubregionModel> _subregions;
        public ObservableCollection<SubregionModel> Subregions
            get { return _subregions; }
                _subregions = value;
                NotifyPropertyChanged(m => m.Subregions);
  • Custom validator looks like:
  • public class PercentValidationAttribute : ValidationAttribute
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
                var items = value as ObservableCollection<SubregionModel>;
                if (items !=null && items.Sum(sr => sr.SubregionPercent) == 100.00)
                    return ValidationResult.Success;
                    return new ValidationResult("Subregions do not add up to 100%");
  • When I run the project I get a red boundary around the Listbox even if the sum is 100. This happens because it does the validation only once during the startup and subsequent changes to the percentage value does not fire up the ValidateProperty event

Any suggestions?

Thanking you in advance.

I have also posted this problem here.

Aug 19, 2011 at 8:17 AM
Edited Aug 19, 2011 at 8:18 AM

OK, after a long agony, I managed to solve this. The solution is listed in Forum.

Sep 13, 2011 at 5:14 AM

Thank you for posting your solution, it helped me with a problem.