将 ArrayList 数据绑定到 VB.NET 中的 ListBox? [英] Databinding an ArrayList to a ListBox in VB.NET?

查看:44
本文介绍了将 ArrayList 数据绑定到 VB.NET 中的 ListBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.NET 工作

I'm working in VB.NET

我有一个名为 Invoices 的 ArrayList,其中填充了类 Invoice 的对象.

I have an ArrayList named Invoices populated with objects of the class Invoice.

我想将此数据绑定到 ListBox,以便随着 ArrayList 的内容更新和更改 ListBox 更新.我已经在 Invoice 类上实现了一个 .ToString 函数,我只是不知道如何将 ArrayList 绑定到 ListBox.

I'd like to data bind this to a ListBox so that as the contents of the ArrayList are updated and changed the ListBox updates. I've implemented a .ToString function on the Invoice class, I just don't know how I'd go about binding the ArrayList to the ListBox.

有什么建议吗?

推荐答案

我将假设这是 winforms.

I'm going to make the assumption that this is winforms.

如果你想要双向数据绑定,你需要做一些事情:

If you want two-way data-binding, you need a few things:

  • 要检测添加/删除等,您需要一个实现 IBindingList 的数据源;对于类,BindingList 是显而易见的选择(ArrayList 根本不会做...)
  • 要检测对象属性的更改,您需要实现INotifyPropertyChanged(通常您可以使用*Changed"模式,但这不受BindingList)
  • to detect addition/removal etc, you need a data-source that implements IBindingList; for classes, BindingList<T> is the obvious choice (ArrayList simply won't do...)
  • to detect changes to properties of the objects, you need to implement INotifyPropertyChanged (normally you can use the "*Changed" pattern, but this isn't respected by BindingList<T>)

幸运的是,ListBox 可以处理这两种情况.一个完整的例子如下;我用过 C#,但概念是一样的...

Fortunately, ListBox handles both of these. A full example follows; I've used C#, but the concepts are identical...

using System;
using System.ComponentModel;
using System.Windows.Forms;
class Data : INotifyPropertyChanged{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this,
            new PropertyChangedEventArgs(propertyName));
    }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Button btn1, btn2;
        BindingList<Data> list = new BindingList<Data> {
            new Data { Name = "Fred"},
            new Data { Name = "Barney"},
        };
        using (Form frm = new Form
        {
            Controls =
            {
                new ListBox { DataSource = list, DisplayMember = "Name",
                     Dock = DockStyle.Fill},
                (btn1 = new Button { Text = "add", Dock = DockStyle.Bottom}),
                (btn2 = new Button { Text = "edit", Dock = DockStyle.Bottom}),
            }
        })
        {
            btn1.Click += delegate { list.Add(new Data { Name = "Betty" }); };
            btn2.Click += delegate { list[0].Name = "Wilma"; };
            Application.Run(frm);
        }
    }
}

这篇关于将 ArrayList 数据绑定到 VB.NET 中的 ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆