gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:06 25 4
gpt4 key购买 nike

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

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

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

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

这是我的 View 模型

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

关于c# - 如何强制对集合中的所有元素进行错误验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com