2017-02-13 3 views
0

Ich habe ein paar verschiedene Wörterbuch-Strukturen, die ich in einer ComboBox anzeigen möchte.Zeige Mitglied im Wörterbuch basierend auf Schlüssel (ComboBox)

In JumpType.cs:

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

Das Wörterbuch Struktur etwas wie folgt aussehen:

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

ich zwei Comboboxen wie dies in meinem UI erstellt haben:

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

In 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"; 
      var selectedValues = jt.jumpCombination //here i'm trying to access value 
        .Where(j => j.Key == Convert.ToInt32(comboJumpComboKey.SelectedItem.Value)) 
        .Select(a => a.Value) 
        .ToList(); 
} 

Wie würde ich über gehen, um die entsprechenden Werte wählen entsprechend dem gewählten Schlüssel?

Vielen Dank im Voraus. Wie Sie auf dem Bild sehen können, wird der Schlüssel angezeigt (1), aber ich kann nichts aus dem Combobox darunter auswählen. comboBox

+0

Was Sie tun möchten, ist die Liste der zweiten comboBox ändert, wann immer Der Index des ersten wird geändert. Sie können also einen Event-Handler für das geänderte Ereignis 'comboBoxJumpComboKey' hinzufügen. In diesem Fall ändern Sie die 'DataSource' von' comboBoxJumpComboValue' – Everyone

+0

@ Everyone ja genau. Ich weiß nicht wirklich, wie ich das machen soll. Würde es dir etwas ausmachen, mir hier eine helfende Hand zu geben? – Joel

+0

Sie verwenden WPF oder WinForms? – Everyone

Antwort

2

Ich würde die Dictionary als Teil der UI-Klasse selbst initialisieren.

public SortedDictionary<int, List<string>> jumpCombination; 
    public Form1() { 
     InitializeComponent(); 
     jumpCombination = new SortedDictionary<int, List<string>>(); 
     // do whatever needed to populate the dictionary here 
     // now add the DataSource as the Keys of your dictionary which are integers 
     comboBoxJumpComboKey.DataSource = new BindingSource(jumpCombination.Keys, null); 
    } 

Dann Doppelklick auf Ihre comboBoxJumpComboKey in Ihrem UI-Designer, eine neue Methode würde kommen, füllen Sie es mit diesem:

private void comboBoxJumpComboKey_SelectedIndexChanged(object sender, EventArgs e) { 
     comboBoxJumpComboValue.DataSource = jumpCombination[int.Parse(comboBoxJumpComboKey.Items[comboBoxJumpComboKey.SelectedIndex].ToString())]; 
    } 
+0

Ich bekomme "InvalidArgument = Wert von -1 ist nicht gültig für 'Index' – Joel

+0

Ist Ihr Wörterbuch bevölkert? Hat es negative Zahlen als Schlüssel? Wenn Sie können 'comboBoxJumpComboValue.DataSource = jumpCombination [comboBoxJumpComboKey.SelectedIndex];' – Everyone

+0

ja, ich rufe es nach der init-Funktion. Und es ist der Schlüssel ab sofort. Aber von diesem Schlüssel-Wert. Ich will in der Lage sein, die Werte auszuwählen, die der Schlüssel enthält (in der Liste) – Joel

Verwandte Themen