Aufgabe: Wie kann man die Farbe von einzelnen Zeilen in einer Dropdown Liste / WPF Combobox dynamisch einstellen?
Lösung:
Man kann innerhalb der WPF Combobox über Combox.ItemContainerStyle->Style ComboboxItem->mit Setter die Foreground und Background Farbe setzen
<ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="Background" Value="{Binding Farbe}" /> <Setter Property="Foreground" Value="{Binding Textfarbe}" /> </Style> </ComboBox.ItemContainerStyle>
|
In der Anwendung sind dann die Farben gebunden an die Daten in einer Tabelle
Die Farbe und die Hintergrundfarbe steht dabei in einer Tabelle.
<ComboBox x:Name="CbxRevisionsgrund" Width="200" Margin="0,0,5,0" SelectionChanged="CbxRevisionsgrund_SelectionChanged" > <ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="Background" Value="{Binding Farbe}" /> <Setter Property="Foreground" Value="{Binding Textfarbe}" /> </Style> </ComboBox.ItemContainerStyle>
</ComboBox>
|