1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
| private void ComboBox_Loaded(object sender, RoutedEventArgs e) { cb.Items.Add("Antarktika"); cb.Items.Add("Nordamerika"); cb.Items.Add("Südamerika");
Debug.Print($"Aktuelle Anzahl der Elemente : {cb.Items.Count}"); for (int i = 0; i < cb.Items.Count; i++) { Debug.Print($" > Schleifenzähler : {i}"); Debug.Print($" > Element : {cb.Items[i]}"); Debug.Print($" > Typ : {cb.Items[i].GetType()}"); ComboBoxItem ci = cb.Items[i] as ComboBoxItem; Debug.Print($" > ComboBoxItem : {(ci != null)}"); if (ci != null) { if (i % 2 == 0) { ci.Background = Brushes.Red; } else { ci.Background = Brushes.Yellow; } }
Debug.Print(new String('-', 50)); }
AddNewComboboxItem("Europa", Brushes.Red); AddNewComboboxItem("Asien", Brushes.Yellow); AddNewComboboxItem("Afrika", Brushes.Red); AddNewComboboxItem("Australien", Brushes.Yellow); }
private void AddNewComboboxItem(string content, Brush brushes) { ComboBoxItem newItem = new ComboBoxItem(); newItem.Content = content; newItem.Background = brushes;
cb.Items.Add(newItem); } } |