Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In view i have datagrid which is bound to ObservableCollection<products> property, SelectedItem which is bound to SelectedProduct property, and text boxes with Text bound to SelectedProduct child properties (Id,Code,Name,QTY,Price).
How to implement INotifyDataErrorInfo on child properties?

What I have tried:

ViewModel:
public class NewViewModel:ObservableObject,INotifyDataErrorInfo
    {
        private readonly Dictionary<string, List<string>> _propertyErrors = new Dictionary<string, List<string>>();
        public bool HasErrors => _propertyErrors.Any();

        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
        public IEnumerable GetErrors(string propertyName)
        {
            return _propertyErrors.GetValueOrDefault(propertyName, null);
        }

        private void AddError(string propertyName, string errorMessage)
        {
            if (!_propertyErrors.ContainsKey(propertyName))
            {
                _propertyErrors.Add(propertyName, new List<string>());
            }

            _propertyErrors[propertyName].Add(errorMessage);
            OnErrorsChanged(propertyName);
        }

        private void ClearErrors(string propertyName)
        {
            if (_propertyErrors.Remove(propertyName))
            {
                OnErrorsChanged(propertyName);
            }

        }

        private void OnErrorsChanged(string propertyName)
        {
            ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
        }


        private ObservableCollection<Products> listProducts;

        public ObservableCollection<Products> ListProducts
        {
            get { return listProducts; }
            set
            {
                listProducts = value;
            }
        }

        private Products selectedProduct;

        public Products SelectedProduct
        {
            get 
            {
                return selectedProduct;
            }
            set
            {
                selectedProduct = value;
                OnPropertyChanged(nameof(SelectedProduct));
            }
        }

        private readonly ProductsService productsService;

        private void All()
        {
            ListProducts.Clear();
            IEnumerable<Products> list = productsService.GetAll();
            foreach (var item in list)
            {
                ListProducts.Add(item);
            }
        }

        public NewViewModel()
        {
            productsService = new ProductsService();
            ListProducts = new ObservableCollection<Products>();
            SelectedProduct = new Products();
            All();
        }
    }
Posted
Updated 25-Jan-21 4:54am
v2
Comments
[no name] 25-Jan-21 11:30am    
https://kmatyaszek.github.io/wpf%20validation/2019/03/13/wpf-validation-using-inotifydataerrorinfo.html
Gruja82 25-Jan-21 12:11pm    
That example is ok for "regular" properties but in my case with child properties it doesn't work. For example i want to add error on SelectedProduct.Code if it is null or white space - doesn't work.
[no name] 25-Jan-21 13:11pm    
It operates at the "class" level. A "child property" is not a class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900