需要手动将 observablecollection 填充到列表视图或任何可以显示数据的元素中
Need a hand filling observablecollection into listview or any element that can show data
正如问题所说,我一直在用我的内容填充数据容器 (listview),希望有人能告诉我!
这是我拥有的类(我的搜索框发送了一个我想在数据库中查找的参数名称,对我的数据库的查询填充并检索了一个 ObservableCollection,其中填充了尽可能多的参数,然后我想把它显示到我的列表视图中,希望有人能帮助我!提前致谢!)
public class LobbyUserViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private DbOperations.DbOperations db;
private ObservableCollection<Person> persons;
public LobbyUserViewModel()
{
persons = new ObservableCollection<Person>();
db = new DbOperations.DbOperations();
searchPersonCommand = new RelayCommand(SearchPerson, CanSearchPerson);
}
public ICommand SearchPersonCommand
{
get { return searchPersonCommand; }
}
public bool CanSearchPerson(object obj)
{
if (SearchPersonBox != string.Empty)
return true;
return false;
}
public void SearchPerson(object obj)
{
Persons = db.DadosForSearchPerson(searchPerson);
if (Persons != null)
{
foreach (var item in Persons)
{
MessageBox.Show(item.Name + " - " + item.Email);
// This works, but I need these 2 items added on a listview
}
}
else
{
MessageBox.Show("Noone found with that name");
}
}
public string SearchPersonBox
{
get { return searchPerson.Name; }
set
{
searchPerson.Name = value;
OnPropertyChanged("SearchPerson");
}
}
public ObservableCollection<Person> Persons
{
get { return persons; }
set { persons = value; }
}
您需要在 Persons
setter 中加注 PropertyChanged。目前,每次调用 SearchPerson()
时 ObservableCollection
都会被替换,但由于您没有引发 PropertyChanged
,WPF 无法知道它已更改。
由于您每次都在替换集合,因此简单地使用 List<T>
可能更有效。 ObservableCollection<T>
仅当您要动态地成为现有集合中的 adding/removing/moving 项时才需要。
GazTheDestroyer+1。没有 OnPropertyChanged 的解决方案类似于:
public void SearchPerson(object obj)
{
if(!CanSearchPerson(obj))
return;
Persons.Clear();
var result = db.DadosForSearchPerson(searchPerson);
if (result != null)
{
foreach (var item in result )
{
MessageBox.Show(item.Name + " - " + item.Email);
// This works, but I need these 2 items added on a listview
Persons.Add(item)
}
}
else
{
MessageBox.Show("Noone found with that name");
}
}
你的Xaml
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="140" Header="Email" DisplayMemberBinding="{Binding Email}" />
</GridView>
</ListView.View>
</ListView>
正如问题所说,我一直在用我的内容填充数据容器 (listview),希望有人能告诉我!
这是我拥有的类(我的搜索框发送了一个我想在数据库中查找的参数名称,对我的数据库的查询填充并检索了一个 ObservableCollection,其中填充了尽可能多的参数,然后我想把它显示到我的列表视图中,希望有人能帮助我!提前致谢!)
public class LobbyUserViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private DbOperations.DbOperations db;
private ObservableCollection<Person> persons;
public LobbyUserViewModel()
{
persons = new ObservableCollection<Person>();
db = new DbOperations.DbOperations();
searchPersonCommand = new RelayCommand(SearchPerson, CanSearchPerson);
}
public ICommand SearchPersonCommand
{
get { return searchPersonCommand; }
}
public bool CanSearchPerson(object obj)
{
if (SearchPersonBox != string.Empty)
return true;
return false;
}
public void SearchPerson(object obj)
{
Persons = db.DadosForSearchPerson(searchPerson);
if (Persons != null)
{
foreach (var item in Persons)
{
MessageBox.Show(item.Name + " - " + item.Email);
// This works, but I need these 2 items added on a listview
}
}
else
{
MessageBox.Show("Noone found with that name");
}
}
public string SearchPersonBox
{
get { return searchPerson.Name; }
set
{
searchPerson.Name = value;
OnPropertyChanged("SearchPerson");
}
}
public ObservableCollection<Person> Persons
{
get { return persons; }
set { persons = value; }
}
您需要在 Persons
setter 中加注 PropertyChanged。目前,每次调用 SearchPerson()
时 ObservableCollection
都会被替换,但由于您没有引发 PropertyChanged
,WPF 无法知道它已更改。
由于您每次都在替换集合,因此简单地使用 List<T>
可能更有效。 ObservableCollection<T>
仅当您要动态地成为现有集合中的 adding/removing/moving 项时才需要。
GazTheDestroyer+1。没有 OnPropertyChanged 的解决方案类似于:
public void SearchPerson(object obj)
{
if(!CanSearchPerson(obj))
return;
Persons.Clear();
var result = db.DadosForSearchPerson(searchPerson);
if (result != null)
{
foreach (var item in result )
{
MessageBox.Show(item.Name + " - " + item.Email);
// This works, but I need these 2 items added on a listview
Persons.Add(item)
}
}
else
{
MessageBox.Show("Noone found with that name");
}
}
你的Xaml
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="140" Header="Email" DisplayMemberBinding="{Binding Email}" />
</GridView>
</ListView.View>
</ListView>