简体   繁体   中英

How can I force error validation for the all elements in collection?

I have a collection of objects and their "valid" valid state depends on different properties, unrelated from the collection.

So, When I change the condition and add a new item to the collection, I got the item with error, but the "valid" state of previous items in the collection do not change.

How can I force the revalidate the whole collection when I change the error property.

I create a sample code here, When I check the Error checkbox, I want to revalidate all items in the collection.

Here is my view model

    public class ViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Person> people;
        public ObservableCollection<Person> People
        {
            get { return people; }
            set
            {
                people = value;
                NotifyPropertyChanged("People");
            }
        }
        private bool _flag;



        public bool IsVisible
        {
            get
            {
                foreach (var person in People)
                {
                    if (person.Age < 18)
                    {
                        return true;
                    }

                }

                return false;
            }
        }

        public ViewModel()
        {

            People = new ObservableCollection<Person>()
            {
              new Person(this) { Name = "Stamat", Age = 20, Height = 1.55 },
              new Person(this) { Name = "Gosho", Age = 21, Height = 1.65 },
              new Person(this) { Name = "Pesho", Age = 22, Height = 1.92 }
        };

            foreach (Person person in People)
            {
                person.PropertyChanged += Person_PropertyChanged;
            }

        }


        private void Person_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged("AreAllLocalized");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));

            }

            if (info.Equals("People"))
            {
                CollectionViewSource.GetDefaultView(this.People).Refresh();
            }
        }
        public bool Flag
        {
            get { return _flag; }
            set
            {
                _flag = value;
                NotifyPropertyChanged("Flag");
                NotifyPropertyChanged("People");
                NotifyPropertyChanged("");

            }
        }

        public void AddPerson()
        {
            People.Add(new Person(this) { Name = "Test", Age = 15, Height = 1.55 });
            People[0].Age = 1000;
            NotifyPropertyChanged("");
            NotifyPropertyChanged(nameof(IsVisible));
        }
    }


    public class Person : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _name;
        private int _age;
        private double _height;
        private ViewModel _model;

        public Person(ViewModel model)
        {
            _model = model;
        }

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                this.NotifyPropertyChanged("Name");
            }
        }

        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
                this.NotifyPropertyChanged("Age");
            }
        }
        public double Height
        {
            get { return _height; }
            set
            {
                _height = value;
                this.NotifyPropertyChanged("Height");
            }
        }

        public Profession Profession
        {
            get { return _profession; }
            set
            {
                _profession = value;
                this.NotifyPropertyChanged("Profession");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                if (columnName == "Name")
                {
                    if (_model.Flag)
                        return "Error selected";
                }
                return result;
            }
        }
    }

View

  <igDP:XamDataGrid Name="grid" DataSource="{Binding Path=People}" Grid.Row="0">
            <igDP:XamDataGrid.FieldLayoutSettings>
                <igDP:FieldLayoutSettings AutoGenerateFields="False"
                                          SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="ErrorIconAndHighlight"
                                          />
            </igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:Field Name="Name"/>
                    <igDP:Field Name="Age" />
                    <igDP:Field Name="Height"/>
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>

 <StackPanel Grid.Row="1">
            <CheckBox Name="Error" Content="Error" Grid.Row="1" IsChecked="{Binding Flag,  Mode=TwoWay}"/>
<Button Content="AddItem" Width="100" Height="22" Click="ButtonBase_OnClick"></Button>
     </StackPanel>

How can I force revalidate all element in the observable collection when I change the Flag property? OnPropertyCahnged("") did not worked on my case.

您可以遍历people并为每个Person调用NotifyPropertyChanged

foreach(var p in people) { p.NotifyPropertyChanged(null); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM