Autor Beitrag
_mk_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Mi 07.04.21 22:02 
@All.

Seit gestern lese mich in das große Thema Windows Presentation Foundation ein. Neu erlangtes Wissen setze ich gleich in meine bestehende Programme um. Gerne würde ich programmseitig die ComboBox-Elemente unterschiedlich einfärben. Das Thema WPF-Styles ist noch nicht behandelt.

Wie kann ich mein Code vereinfachen? Gerne würde ich über jedes einzelne Element iterieren und es anders einfärben. Leider musste ich feststellen, dass das so einfach für mich als Anfänger nicht möglich, weil das ComboBox-Item eigentlich ein Content-Control ist.

Hier aber mein Code:
ausblenden volle Höhe C#-Quelltext
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()}");
                // Ausgabe : > Typ             : System.String

                ComboBoxItem ci = cb.Items[i] as ComboBoxItem; // Kein 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));
            }

            // Alternative
            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);
        }
    }
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 08.04.21 09:24 
Hallo,

da du bei cb.Items.Add jeweils String-Objekte übergibst, erhältst du beim Auslesen auch wiederum diese.
Daher ist deine Vorgehensweise mittels AddNewComboboxItem schon der bessere Ansatz - du solltest die Aufrufe davon nur dann auch vor der Schleife durchführen.

Mittelfristig solltest du bei WPF aber auf MVVM umsteigen, d.h. Verwendung eines ViewModels und DataBinding.
Schau dir auch mal den Code in WPF Tutorial - The ComboBox control (bes. "Data binding the ComboBox") an.