C# 是否删除datagrid Wpf MVVM中的重复行?
当我从文本框添加新元素时,我试图删除DataGrid中的重复元素。 也许,有人知道如何解决这个问题吗C# 是否删除datagrid Wpf MVVM中的重复行?,c#,wpf,mvvm,datagrid,caliburn.micro,C#,Wpf,Mvvm,Datagrid,Caliburn.micro,当我从文本框添加新元素时,我试图删除DataGrid中的重复元素。 也许,有人知道如何解决这个问题吗 public class ShellViewModel : Screen { private string _input = string.Empty; public ObservableCollection<Person> people; public ShellViewModel() { } public string Inpu
public class ShellViewModel : Screen
{
private string _input = string.Empty;
public ObservableCollection<Person> people;
public ShellViewModel()
{
}
public string Input
{
get
{
return _input;
}
set
{
_input = value;
NotifyOfPropertyChange(() => Input);
}
}
public ObservableCollection<Person> People
{
get
{
return people;
}
set
{
people = value;
NotifyOfPropertyChange(() => People);
}
}
Person person = new Person();
ObservableCollection<Person> persons = new ObservableCollection<Person>();
public void Write()
{
person.name = Input;
persons.Add(person);
People = persons;
}
}
公共类ShellViewModel:屏幕
{
私有字符串_input=string.Empty;
公众观察收集人员;
公共ShellViewModel()
{
}
公共字符串输入
{
得到
{
返回输入;
}
设置
{
_输入=值;
NotifyOfPropertyChange(()=>输入);
}
}
公众观察收集人
{
得到
{
还人,;
}
设置
{
人=价值;
财产变更通知(()=>人);
}
}
Person=新人();
ObservableCollection persons=新的ObservableCollection();
公共空写()
{
person.name=输入;
人。添加(人);
人=人;
}
}
我在向DataGrid添加元素方面没有任何问题,但我不知道如何删除重复的元素。
提前谢谢。您可以避免添加重复记录
public void Write()
{
person.name = Input;
if (!persons.Contains(person))
persons.Add(person);
People = persons;
}
在将
人员添加到集合之前,必须检查该人员是否已存在
我还修复了代码中的一些问题。由于People
是一个observedcollection
,您可以直接向其中添加项目,视图将注册集合中的更改并立即更新。
将Person
添加到集合时,还应创建一个新实例,否则将覆盖现有实例。您当前使用的是单个人员
。字段应始终为私有
public class ShellViewModel : Screen
{
public void ShellViewModel()
{
this.Input = string.Empty;
this.People = new ObservableCollection<People>();
}
private string _input = string.Empty;
public string Input
{
get => _input;
set
{
_input = value;
NotifyOfPropertyChange(() => Input);
}
}
private ObservableCollection<Person> people;
public ObservableCollection<Person> People
{
get => people;
set
{
people = value;
NotifyOfPropertyChange(() => People);
}
}
public void Write()
{
if (string.IsNullOrWhiteSpace(this.input)
|| this.People.Any(person => person.name.Equals(this.Input, StringComparison.OrdinalIgnoreCase))
{
return;
}
Person newPerson = new Person() {name = this.Input};
this.People.Add(newPerson);
}
}
公共类ShellViewModel:屏幕
{
public void ShellViewModel()
{
this.Input=string.Empty;
this.People=新的可观察集合();
}
私有字符串_input=string.Empty;
公共字符串输入
{
获取=>\u输入;
设置
{
_输入=值;
NotifyOfPropertyChange(()=>输入);
}
}
私人收集人员;
公众观察收集人
{
get=>人;
设置
{
人=价值;
财产变更通知(()=>人);
}
}
公共空写()
{
if(string.IsNullOrWhiteSpace(this.input)
||this.People.Any(person=>person.name.Equals(this.Input,StringComparison.OrdinalIgnoreCase))
{
返回;
}
Person newPerson=newPerson(){name=this.Input};
this.People.Add(newPerson);
}
}
它只对第一个元素有效。当我向DataGrid输入第二个元素时,DataGrid不显示其他元素,除了first,当我使用“if(this.People.Any(person=>person.name.Equals(this.input,StringComparison.OrdinalIgnoreCase)){return;}”时,我有System.ArgumentNullException您需要初始化属性。我跳过了它,因为我认为这应该是清楚的。我更新了代码。