如何强制对集合中的所有元素进行错误验证?

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

我有一个对象集合,它们的 "valid" 有效状态取决于不同的属性,与集合无关。

因此,当我更改条件并将新项目添加到集合中时,我得到了错误的项目,但集合中先前项目的 "valid" 状态没有改变。

如何在更改错误时强制重新验证整个集合 属性。

我在这里创建了一个示例代码,当我选中错误复选框时,我想重新验证集合中的所有项目。

这是我的视图模型

    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;
            }
        }
    }

查看

  <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>

如何在更改 Flag 属性 时强制重新验证可观察集合中的所有元素? OnPropertyCahnged("") 对我的情况无效。

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

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