将字符串集合绑定到 ListView、Windows 窗体

发布于 2024-09-11 19:00:06 字数 603 浏览 3 评论 0原文

我有一个 StringCollection,我想以一种方式绑定到 ListView。如图所示,ListView 应显示 StringCollection 的内容。我将以编程方式从集合中删除项目,这样它们就不需要通过 ListView 与之交互。

我有一个带有属性的表单,就像这样 -->

public DRIUploadForm()
    {
        InitializeComponent();

        lvwDRIClients.DataBindings.Add("Items", this.DirtyDRIClients, "DirtyDRIClients");
    }

private StringCollection _DirtyDRIClients;
public StringCollection DirtyDRIClients 
    { 
        get
        {
            return _DirtyDRIClients;
        }
        set
        {
            _DirtyDRIClients = Settings.Default.DRIUpdates;
        }
    }

I have a StringCollection that I want to One Way Bind to a ListView. As in, the ListView should display the contents of the StringCollection. I will be removing items from the collection programatically so they don't need to interact with it through the ListView.

I have a Form with a Property, like so -->

public DRIUploadForm()
    {
        InitializeComponent();

        lvwDRIClients.DataBindings.Add("Items", this.DirtyDRIClients, "DirtyDRIClients");
    }

private StringCollection _DirtyDRIClients;
public StringCollection DirtyDRIClients 
    { 
        get
        {
            return _DirtyDRIClients;
        }
        set
        {
            _DirtyDRIClients = Settings.Default.DRIUpdates;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

终遇你 2024-09-18 19:00:06

您实际上无法绑定到 ListView 控件,因为它不支持绑定。您需要以编程方式添加项目。但是,您可以绑定到 ListBox,尽管正如其他人所说,您不能直接绑定字符串,您需要为它们创建一个包装器。像这样的东西...

public partial class Form1 : Form
{
    List<Item> items = new List<Item>
    {
        new Item { Value = "One" },
        new Item { Value = "Two" },
        new Item { Value = "Three" },
    };

    public Form1()
    {
        InitializeComponent();

        listBox1.DataSource = items;
        listBox1.DisplayMember = "Value";
    }
}

public class Item
{
    public string Value { get; set; }
}

You cannot actually bind to a ListView control, as it does not support binding. You need to add the items programmatically. You can however bind to a ListBox, although as others have said you cannot bind strings directly, you need to create a wrapper for them. Something like this...

public partial class Form1 : Form
{
    List<Item> items = new List<Item>
    {
        new Item { Value = "One" },
        new Item { Value = "Two" },
        new Item { Value = "Three" },
    };

    public Form1()
    {
        InitializeComponent();

        listBox1.DataSource = items;
        listBox1.DisplayMember = "Value";
    }
}

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