2012-03-28 110 views
1

我有兩個列表,第一個(右)代表所有汽車和secound(左)列表的待售汽車列表。添加/刪除列表<>在列表框中選擇的項目

在我的控制下有兩個ListBox,他們每個人都會顯示一個汽車列表。我想點擊一個按鈕(< <),列表框1中選定的車將被添加到列表框1列表中。

這兩個列表是在控件類之外創建的,所以我需要綁定到ListBoxes。我試圖使用DataSource,但如果我設置它,我不能使用從項目中添加刪除。哪種做法最好?

enter image description here

謝謝。 OBS:Ive已更改爲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++; 
      } 
     } 
    } 
+0

你有沒有嘗試設置數據源,並從列表中刪除自身的項目? – 2012-03-28 12:32:12

+0

是的,但一旦我設置DataSource,它不會再更新,如果我更改orignal列表,組合框仍舊替代舊的,我試圖重置DataSource,但沒有任何變化。 – Pedro77 2012-03-28 12:35:29

+0

您使用哪種類型的Windows應用程序? (Winform/SilverLight等)。 – 2012-03-28 12:37:34

回答

2

一旦設置了數據源,就不能將項目添加到該集合中。

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

但是你可以用一些變通方法

1)保存到數據庫,做到這一點,並用新值重新加載它,並將其綁定

OR

2)獲取Listbox的數據源並將其存儲在變量中,並添加一個新項目(從所選項目創建)和d然後將其綁定再次

例重(類是針對我的需要,您可以自定義基於你的類結構)

//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"; 
+0

重新綁定數據源是我錯過了。 DataSource = null和DataSource = myList。 – Pedro77 2012-03-28 12:49:43

+0

@ Pedro77:很高興它爲你準備。 – Shyju 2012-03-28 12:52:19

0

您可以創建添加和刪除在列表中的項目您的自定義事件。在您定義組合框的控件類中處理這些事件以添加或刪除其項目。

+0

您的意思是在控件上創建一個公共事件並綁定它以添加/刪除項目。是的,這是一種可能性,但我想改變列表中的列表。 – Pedro77 2012-03-28 12:37:29

0

用數據源綁定列表,就像你一樣。然後刪除並添加項目列表,而不是組合框自己。

相關問題