【问题标题】:add/remove List<> items selected in a ListBox添加/删除 List<> 在 ListBox 中选择的项目
【发布时间】:2012-03-28 12:20:38
【问题描述】:

我有两个列表,第一个(右)表示所有汽车的列表,第二个(左)表示待售汽车的列表。

在我的控制中有两个 ListBox,每个都将显示一个汽车列表。我想单击一个按钮 (

这两个列表是在控件类之外创建的,所以我需要绑定到 ListBoxes。我曾尝试使用 DataSource,但如果我设置它,我将无法使用 Add Remove from Items。最好的方法是什么?

谢谢。

OBS:我已更改为 ListBox。


我正在使用的解决方案是:非常非常丑陋的解决方案...:/

    public IList<Item> ItensToMaintaim
    {
        get { return (IList<Item>)this.itensToMainTainList.DataSource; }
        set
        {
            //Need to set null to refresh
            this.itensToMainTainList.DataSource = null;
            this.itensToMainTainList.DataSource = value;
            this.itensToMainTainList.DisplayMember = "Name";
            this.itensToMainTainList.ValueMember = "Name";
        }
    }

    public IList<Item> Itens
    {
        get { return (IList<Item>)this.itensList.DataSource; }
        set
        {
            //Need to set null to refresh
            this.itensList.DataSource = null;
            this.itensList.DataSource = value;
            this.itensList.DisplayMember = "Name";
            this.itensList.ValueMember = "Name";
        }
    }

    private void removeItem_Click(object sender, EventArgs e)
    {
        if (this.itensToMainTainList.SelectedItem != null)
        {
            this.itens2.Remove((Item)this.itensToMainTainList.SelectedItem);
            this.ItensToMaintaim = this.itens2;
            if (this.itensToMainTainList.SelectedIndex < 0)
            {
                this.itensToMainTainList.SelectedIndex = this.itens2.Count - 1;
            }
        }
    }

    private void addItem_Click(object sender, EventArgs e)
    {
        if (this.itensList.SelectedItem != null)
        {
            bool contains = false;
            contains = this.itens2.Contains(this.itensList.SelectedItem);
            if (!contains)
            {
                this.itens2.Add((Item)this.itensList.SelectedItem);
                this.ItensToMaintaim = this.itens2;
            }
            if (this.itensList.SelectedIndex < this.itens1.Count - 1)
            {
                this.itensList.SelectedIndex++;
            }
        }
    }

【问题讨论】:

  • 您是否尝试设置 DataSource 并从列表本身中删除项目?
  • 是的,但是一旦我设置了 DataSource,它就不再更新了,如果我更改了原始列表,组合框仍然会显示旧的,我尝试重置 DataSource,但没有任何变化。
  • 您使用哪种类型的 Windows 应用程序? (Winform / SilverLight 等)。

标签: c# list combobox


【解决方案1】:

一旦您设置了数据源,您就无法将项目添加到该集合中。

"Items collection cannot be modified when the DataSource property is set."

但你可以通过一些解决方法来做到这一点

1) 保存到数据库并使用新值再次加载并绑定它

2) 获取Listbox的现有数据源并将其存储在一个变量中并添加一个新项目(从所选项目创建)然后再次重新绑定它

示例(类是特定于我的需要,您可以根据您的类结构自定义)

    //Take the existing
    List<MailerKit> objExisting = (List<MailerKit>)comboBox1.DataSource;
    //Add the new one
    objExisting.Add(new MailerKit { KitName = comboBox1.SelectedText, ID = Convert.ToInt32(comboBox1.SelectedValue) });

    //Rebind again
    comboBox1.DataSource = objExisting;
    comboBox1.DisplayMember = "KitName";
    comboBox1.ValueMember = "ID";

【讨论】:

  • 重新绑定数据源是我所缺少的。 DataSource = null 和 DataSource = myList。
  • @Pedro77:很高兴它为你播种。
【解决方案2】:

您可以创建自定义事件来添加和删除列表中的项目。在您定义组合框的控件类中处理这些事件以添加或删除其项目。

【讨论】:

  • 您的意思是在控件上创建一个公共事件并将其绑定到添加/删除项目。是的,这是一种可能性,但我正在尝试更改控件内的列表。
【解决方案3】:

与您一样,将列表与数据源绑定。然后删除项目并将其添加到列表中,而不是组合框本身。

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多