簡體   English   中英

根據另一個組合框值更改組合框值?

[英]Change combobox value according to another combobox value?

我對C#和WPF項目有點陌生。 所以這是我的問題。

我有2個組合框,其中包含字符串列表。

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

這是我的代碼:

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向我顯示,當我在第一個組合框中選擇值時,程序在所有情況下都會以隨機方式運行。

幫助將不勝感激,謝謝!

ComboBox有物品。 因此,只需找到所選內容及其標題即可。

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;
        }
}

另外,最好將switch(text)更改為switch(index),以防止單詞中的大小寫不匹配和類型錯誤。
PS對不起我的英語
注意:這用於WPF,不適用於WinForm解決方案

只需通過ObservableCollection<string>更改所有List<string> ObservableCollection<string>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM