删除数据网格 Wpf MVVM 中的重复行?
Delete duplicate rows in datagrid Wpf MVVM?
当我从 TextBox 添加新元素时,我试图删除 DataGrid 中的重复元素。
也许,有人知道如何解决它吗?
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;
}
}
我对向 DataGrid 添加元素没有任何问题,但我不知道如何删除重复的元素。
提前谢谢你。
您可以避免添加重复记录
public void Write()
{
person.name = Input;
if (!persons.Contains(person))
persons.Add(person);
People = persons;
}
您必须先检查 Person
是否已经存在 ,然后才能 将其添加到集合中。
我还修复了您的代码的一些问题。由于 People
是一个 ObservableCollection
,您可以直接向其添加项目,视图将注册集合中的更改并立即更新。
在将 Person
添加到集合中时,您还应该创建一个新的 Person
实例,否则会覆盖现有实例。您目前正在使用单个 Person
。字段应 始终 为 private
.
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);
}
}
当我从 TextBox 添加新元素时,我试图删除 DataGrid 中的重复元素。 也许,有人知道如何解决它吗?
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;
}
}
我对向 DataGrid 添加元素没有任何问题,但我不知道如何删除重复的元素。 提前谢谢你。
您可以避免添加重复记录
public void Write()
{
person.name = Input;
if (!persons.Contains(person))
persons.Add(person);
People = persons;
}
您必须先检查 Person
是否已经存在 ,然后才能 将其添加到集合中。
我还修复了您的代码的一些问题。由于 People
是一个 ObservableCollection
,您可以直接向其添加项目,视图将注册集合中的更改并立即更新。
在将 Person
添加到集合中时,您还应该创建一个新的 Person
实例,否则会覆盖现有实例。您目前正在使用单个 Person
。字段应 始终 为 private
.
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);
}
}