添加到绑定的 ListView

发布于 2024-12-09 13:10:03 字数 2069 浏览 1 评论 0原文

我正在尝试将一个人添加到露营者(人)列表中。我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-16 13:10:03

您不应该刷新数据库来添加camper:

Camper person = new Camper(age, grade, nameMe);
if TryAddToDatabase(person)
{
    bindMe.Campers.Add(person);
}

如果ListView绑定到bindMe.Campers,它会自动刷新并显示新项目。我还建议您完整阅读WPFTutorial,这样您将来就不会做任何额外的工作。

You shouldn't refresh the database to add camper:

Camper person = new Camper(age, grade, nameMe);
if TryAddToDatabase(person)
{
    bindMe.Campers.Add(person);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文