【问题标题】:displaying members of dictionary depending on key in comboBox根据组合框中的键显示字典成员
【发布时间】:2017-02-10 10:43:32
【问题描述】:

我想在组合框中显示几个不同的字典结构。

在 JumpType.cs 中:

public SortedDictionary<int, List<string>> jumpCombination = new SortedDictionary<int, List<string>>(); 

字典结构如下所示:

Key    Values
1      Flygande
       EjFlygande
2      Bak
       Pik
       Test
3      ...

我在我的 UI 中创建了两个组合框,如下所示:

Select Key:      _____________
                |   ComboBox  |
                --------------      __________
                 _____________      |   OK   |
Select Value:   |   ComboBox  |     ----------
                --------------

在 Form1.cs 中

 InitializeComponent();
 JumpType jt = new JumpType();
 jt.addjumpCombination(); // populating the dictionary
 if (jt.jumpCombination != null)
 {
    comboBoxJumpComboKey.DataSource = new BindingSource(jt.jumpCombination, null); // Key => null
    comboBoxJumpComboKey.DisplayMember = "Value";
    comboBoxJumpComboKey.ValueMember = "Key";
    comboBoxJumpComboValue.DisplayMember = "Value";
 }

我将如何根据所选键选择相应的值?

提前致谢。

【问题讨论】:

  • 列表 values = jumpCombination[key]

标签: c# .net dictionary combobox


【解决方案1】:

您可以使用以下 LINQ 查询过滤您的列表:

var selectedValues = jumpCombination
                    .Where(j => j.Key == Convert.ToInt32(comboBoxJumpComboKey.SelectedItem.Value))
                    .Select(a => a.Value)
                    .ToList();

此外, selectedValues 是 List 的集合,在您的情况下是 Values

【讨论】:

  • 不幸的是不起作用。 SelectedItem.Value 语法无效:/
  • 奇怪! comboBoxJumpComboKey​​ 不是组合框吗?那么 comboBoxJumpComboKey​​.SelectedValue 呢?这对你有用吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2019-02-01
  • 1970-01-01
  • 2013-07-26
相关资源
最近更新 更多