2015-11-13 59 views
2

我想動態更改CheckedListBox中的項目源並保持其選定的值?更改CheckedListBox ItemsSource

CheckedListBox1 | CheckedListBox2 
[x] list0  | [ ] list0item0 
[ ] list1  | [ ] list0item1 
[ ] list2  | [ ] list0item2 
[ ] list3  | [ ] list0item3 

,並選擇列表1時(未選中,只是highligted)更新CheckedListBox2項目

CheckedListBox1 | CheckedListBox2 
[ ] list0  | [ ] list1item0 
[x] list1  | [ ] list1item1 
[ ] list2  | [ ] list1item2 
[ ] list3  | [ ] list1item3 

這裏是一個picture描述我的問題。

這裏是一個代碼片段:

public void customModuleFunctionsCheckedListBox_SelectedIndexChanged(object sender, EventArgs e) 
      { 
       //checks before calling this function if there is any element selected.. 
       for (int i = 0; i < this.mainForm.customFunctionList[index].Items.Count; i++) 
       { 
        if (this.mainForm.customFunctionList[index].SelectedIndex == i) 
        { 
         this.mainForm.customFunctionUseCasesList[index].Items.Clear(); 
//this.mainForm.customFunctionUseCasesList[index].ItemsSourceOrWhateverMethodIs = aListOfStrings.... 

        } 
       } 
      } 

對此有任何清晰的解決方案?提前致謝!

回答

0

我自己的問題的答案可能是這樣的。 但我需要在CheckedListBox1中放置字符串而不是(集合)。

public partial class Form1 : Form 
    { 
     List<List<object>> function; 
     List<object> useCase; 

     public Form1() 
     { 
      function = new List<List<object>>(); 
      useCase =new List<object>(); 

      InitializeComponent(); 


      useCase.AddRange(new object[] { 
      "List0Item0", 
      "List0Item1", 
      "List0Item2", 
      "List0Item3", 
      "List0Item4", 
      "List0Item5"}); 

      this.function.Add(this.useCase); 

      this.checkedListBox1.Items.Add(function); 

      useCase = new List<object>(); 
      useCase.AddRange(new object[] { 
      "List1Item0", 
      "List1Item1", 
      "List1Item2", 
      "List1Item3", 
      "List1Item4", 
      "List1Item5"}); 

      this.function.Add(this.useCase); 

      this.checkedListBox1.Items.Add(function); 


     } 

     private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      this.checkedListBox2.Items.Clear(); 
      for (int i = 0; i < this.function.Count(); i++) 
      { 
       if (checkedListBox1.SelectedIndex == i) 
       { 
        for (int j = 0; j < this.function[i].Count(); j++) 
        { 
         this.checkedListBox2.Items.Add(this.function[i][j]); 
        } 
       } 
      } 
     } 
    } 

代碼輸出附here

0

我不是c#的專家,更像是一個新手! 但我以這種方式想到了這一點。 當某人從listBox1中選擇一個值(單擊)時,它將清除listBox2結果以及默認或先前查詢完成的查詢。 在選擇listBox1項目之後,listBox2 onClick將根據選擇的listBox1項目進行查詢。

if(listBox1.Selecteditem == "Fruit") 
{ 
    query to database WHERE item is Fruit 
} 

雖然我無法確認,但是我發現了一些關於這種列表框選擇的東西的文檔。

Microsoft ListBox Selected items documentation

希望這有助於! :)

+0

對不起,我的問題。我不小心說listbox而不是checkedlistbox:「)對不起! –

相關問題