【问题标题】:Change combobox value according to another combobox value?根据另一个组合框值更改组合框值?
【发布时间】:2017-01-30 08:53:55
【问题描述】:

我对 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 告诉我,当我在第一个组合框中选择我的值时,如果情况以随机方式出现,程序就会运行。

我们将不胜感激,谢谢!

【问题讨论】:

  • 当您更改选择时,comboSaison 选择的项目是什么?尝试使用 comboSaison.SelectedItem 并找出答案。
  • 它是“Ete 2017”作为选定值,所以它应该是名为comboTheme @PeterB的组合框中的themesE17列表
  • 使用数据绑定我认为更可靠的选择是绑定到 xaml 中的可观察字符串集合,然后更新该集合的内容而不是绑定。

标签: c# wpf data-binding combobox


【解决方案1】:

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),以防​​止单词中的大小写不匹配和错误类型。
p.s.对不起我的英语
注意:这适用于 WPF,不适用于 WinForm 解决方案

【讨论】:

  • 感谢您的回答,我试过了,但似乎缺少引用,因为代码不会执行...
  • comboSaison_SelectionChanged 是否标记为 SelectionChanged 事件?
  • @PeterB 是的。
  • 它根本没有执行?还是在 if(ind
  • @PeterB 暂时没有执行
【解决方案2】:

只需将您所有的List&lt;string&gt; 更改为ObservableCollection&lt;string&gt;

【讨论】:

  • 感谢您的回答,但这并不能解决“AddRange”错误
  • 哦,我以为它工作正常,你在运行时出错了?在哪条线上?
  • 在我自己的代码上是没有运行时错误,只是在我的第一个组合框选择的值上显示随机列表,但是我在使用 sergio 的解决方案@Safe 时出现运行时错误
  • 如果您的代码在您共享时可以正常工作,请返回此版本并尝试将 List 更改为 ObservableCollection 您的代码没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多