添加到绑定的 ListView
我正在尝试将一个人添加到露营者(人)列表中。我已将 ListView(GridView)
绑定到数据库,它显示所有姓名、年龄和成绩。但现在我正在尝试添加一个新人并将他与其他人一起添加(显示)到 ListView
中。非常感谢任何帮助。
这是我所拥有的: ObservableCollection
class BindingCamper
{ // This class assist in binding campers from listview to the textboxes on the camperspage
public ObservableCollection<Camper> Campers { get; private set; }
public BindingCamper()
{
Campers = new ObservableCollection<Camper>();
}
}
这是我将姓名列表添加到列表视图的位置:
MainWindow _parentForm;
public ObservableCollection<Camper> Campers { get; private set; }
public CampersPage(MainWindow parent)
{
_parentForm = parent;
InitializeComponent();
var bindMe = new BindingCamper();
for (int i = 0; i < _parentForm.allCampers.Count; i++)
bindMe.Campers.Add(new Camper { Name = "" + _parentForm.allCampers[i].getName(), Ages = _parentForm.allCampers[i].getAge(), SchoolGrade = _parentForm.allCampers[i].getGrade() });
DataContext = bindMe;
这是我添加新露营者(人)的位置,我正在尝试将他/她添加到列表视图:
String nameMe;
nameMe = txtNewFirstName.Text ;
int age;
int grade;
if (nameMe != "" && IsNumber(txtNewGrade.Text) && IsNumber(txtNewAge.Text))
{
age = Convert.ToInt16(txtNewAge.Text);
grade = Convert.ToInt16(txtNewGrade.Text);
// Create New Camper
Camper person = new Camper(age, grade, nameMe);
_parentForm.allCampers.Add(person);
//_parentForm.camperPage.listViewCampers.Items.Refresh();
var bind = new BindingCamper();
// bind.Campers.Add(new Camper { Name = person.getName(), Ages = person.getAge(), SchoolGrade = person.getGrade() });
// _parentForm.camperPage.Campers.Add(new Camper { Name = person.getName(), Ages = person.getAge(), SchoolGrade = person.getGrade() });
Close();
I'm trying to add a person to a list of campers(people). I have bound the ListView(GridView)
to the database and it displays all the names, ages, and grades. But now I'm trying to add a new person and add(display) him to the ListView
along with everyone else. Any help is greatly appreciated.
Here's what I have: ObservableCollection
class BindingCamper
{ // This class assist in binding campers from listview to the textboxes on the camperspage
public ObservableCollection<Camper> Campers { get; private set; }
public BindingCamper()
{
Campers = new ObservableCollection<Camper>();
}
}
Here is where I add the list of names to the listview:
MainWindow _parentForm;
public ObservableCollection<Camper> Campers { get; private set; }
public CampersPage(MainWindow parent)
{
_parentForm = parent;
InitializeComponent();
var bindMe = new BindingCamper();
for (int i = 0; i < _parentForm.allCampers.Count; i++)
bindMe.Campers.Add(new Camper { Name = "" + _parentForm.allCampers[i].getName(), Ages = _parentForm.allCampers[i].getAge(), SchoolGrade = _parentForm.allCampers[i].getGrade() });
DataContext = bindMe;
Here is where I add a new camper(person) and I'm trying to add him/her to the listview:
String nameMe;
nameMe = txtNewFirstName.Text ;
int age;
int grade;
if (nameMe != "" && IsNumber(txtNewGrade.Text) && IsNumber(txtNewAge.Text))
{
age = Convert.ToInt16(txtNewAge.Text);
grade = Convert.ToInt16(txtNewGrade.Text);
// Create New Camper
Camper person = new Camper(age, grade, nameMe);
_parentForm.allCampers.Add(person);
//_parentForm.camperPage.listViewCampers.Items.Refresh();
var bind = new BindingCamper();
// bind.Campers.Add(new Camper { Name = person.getName(), Ages = person.getAge(), SchoolGrade = person.getGrade() });
// _parentForm.camperPage.Campers.Add(new Camper { Name = person.getName(), Ages = person.getAge(), SchoolGrade = person.getGrade() });
Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该刷新数据库来添加camper:
如果
ListView
绑定到bindMe.Campers,它会自动刷新并显示新项目。我还建议您完整阅读WPFTutorial,这样您将来就不会做任何额外的工作。You shouldn't refresh the database to add camper:
If
ListView
is bounded to bindMe.Campers it would refresh automatically and show new item. I also would recommend you completely read the WPFTutorial so you won't do any extra work in the future.