2017-01-30 166 views
0

我對C#和WPF項目有點新了。所以這是我的問題。根據另一個組合框值更改組合框值?

我有2個Combobox填充字符串列表。

根據我的第一個組合框的值,我想更改 第二個組合框中可用的列表。

這裏是我的代碼:

public partial class MainWindow : Window 
{ 
    //creation de listes 
    List<string> themesE17 = new List<string>(); 
    List<string> themesH17 = new List<string>(); 
    List<string> themesE16 = new List<string>(); 
    List<string> themesH16 = new List<string>(); 



    public MainWindow() 
    { 
     InitializeComponent(); 

     initLists(); 

     string value = comboSaison.Text; 
     Console.WriteLine("The value of season combobox " + value); 

    } 

    public void initLists() 
    { 
     //saison 2017 
     themesE17.Add("Ete 17 Theme1"); 
     themesE17.Add("Ete 17 Theme2"); 

     themesH17.Add("Hiver 17 Theme1"); 
     themesH17.Add("Hiver 17 Theme2"); 

     //saison 2016 
     themesE16.Add("Ete 16 Theme1"); 
     themesE16.Add("Ete 16 Theme2"); 

     themesH16.Add("Hiver 16 Theme1"); 
     themesH16.Add("Hiver 16 Theme2"); 
    } 

    private void comboSaison_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (comboSaison.Text == "Ete 2017") 
     { 
      comboTheme.ItemsSource = themesE17; 
      Console.WriteLine("1st if E17"); 
     } 

     else if (comboSaison.Text == "Hiver 2017") 
     { 
      comboTheme.ItemsSource = themesH17; 
      Console.WriteLine("2nd if H17"); 
     } 

     else if (comboSaison.Text == "Ete 2016") 
     { 
      comboTheme.ItemsSource = themesE16; 
      Console.WriteLine("3rd if E16"); 
     } 

     else if (comboSaison.Text == "Hiver 2016") 
     { 
      comboTheme.ItemsSource = themesH16; 
      Console.WriteLine("4th if H16"); 
     } else 

      Console.WriteLine("Error in selection !"); 
    } 
} 

但它不工作,我Console.WriteLine顯示我那程序進入在所有的情況下,如果在當我在第一個組合框中選擇我的價值觀以隨機的方式。

幫助將不勝感激,謝謝!

+0

什麼是comboSaison選定的項目? 嘗試使用comboSaison.SelectedItem並找出答案。 –

+0

它是「Ete 2017」作爲選定值,所以它應該是combobox中的主題E17列表名爲comboTheme @PeterB – Jay

+0

使用數據綁定我認爲一個更可靠的選項將綁定到xaml中可觀察的字符串集合,然後更新該集合的內容而不是綁定。 – LordWilmore

回答

4

組合框有項目。所以只需找到選定的和它的標題。

private void comboSaison_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var obj = (ComboBox)sender; 
     var ind = obj.SelectedIndex; 
     var selectedItem = (ComboBoxItem)obj.Items[ind]; 
     switch ((string)selectedItem.Content) 
     { 
      case "Ete 2017": 
       comboTheme.Items.Clear(); 
       foreach (var item in themesE17) 
        comboTheme.Items.Add(new ComboBoxItem() { Content = item }); 
       break; 
      case "Hiver 2017": 
       comboTheme.Items.Clear(); 
       foreach (var item in themesH17) 
        comboTheme.Items.Add(new ComboBoxItem() { Content = item }); 
       break; 
      case "Ete 2016": 
       comboTheme.Items.Clear(); 
       foreach (var item in themesE16) 
        comboTheme.Items.Add(new ComboBoxItem() { Content = item }); 
       break; 
      case "Hiver 2016": 
       comboTheme.Items.Clear(); 
       foreach (var item in themesH16) 
        comboTheme.Items.Add(new ComboBoxItem() { Content = item }); 
       break; 
      default: 
       break; 
     } 
} 

此外,更好的切換開關(文本)切換(索引),以防止案件不匹配和單詞misstypes。
p.s.對不起,我的英語
注:此爲WPF,而不是WinForm的解決方案

+0

感謝您的回答,我試了一下,但似乎有一個參考丟失,因爲代碼不會執行... – Jay

+0

using System.IO; – Sergio

+0

對不起,它不起作用 – Jay

0

只要改變當你改變你的選擇你所有的List<string>通過ObservableCollection<string>

+0

感謝您的回答,但是,這並不能解決「AddRange」錯誤 – Jay

+0

哦,我認爲它工作正常,在運行時出現錯誤?在哪一行? – Safe

+0

我自己的代碼是沒有運行時錯誤,只顯示隨機列表在我的第一個組合框選定的值,但我有Sergio的解決方案運行時錯誤@Safe – Jay

相關問題