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

C# 如何对集合中的所有元素强制进行错误验证?,c#,wpf,inotifypropertychanged,idataerrorinfo,C#,Wpf,Inotifypropertychanged,Idataerrorinfo,我有一个对象集合,它们的“有效”有效状态取决于不同的属性,与集合无关 因此,当我更改条件并向集合中添加一个新项时,我得到了有错误的项,但集合中以前项的“有效”状态没有更改 当我更改error属性时,如何强制重新验证整个集合 我在这里创建了一个示例代码,当我选中错误复选框时,我想重新验证集合中的所有项目 这是我的视图模型 public class ViewModel : INotifyPropertyChanged { private ObservableColle

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

因此,当我更改条件并向集合中添加一个新项时,我得到了有错误的项,但集合中以前项的“有效”状态没有更改

当我更改error属性时,如何强制重新验证整个集合

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

这是我的视图模型

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

看法


当我更改Flag属性时,如何强制重新验证可观察集合中的所有元素?
OnPropertyChanged(“”)在我的案例中不起作用。

您可以遍历
人员
,并为每个
人员调用
NotifyPropertyChanged

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

您是否尝试在
人员中迭代
foreach(人中的var p){p.NotifyPropertyChanged(null);}
yep,这很有效。。。非常感谢。
  <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>
foreach(var p in people) { p.NotifyPropertyChanged(null); }