2017-01-30 165 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

相关问题