In WPF, modify the style of The ListBox item and modify the background color of the selected item.
Final effect:
1 <ListBox Name = "cmb"> 2 <! -- Modify color --> 3 <ListBox. Resources> 4 <! -- Highlight background Color --> 5 <SolidColorBrush x: Key = "{x: Static SystemColors. HighlightBrushKey}" Color = "Transparent"/> 6 <! -- Non-highlighted background Color --> 7 <SolidColorBrush x: Key = "{x: Static SystemColors. ControlBrushKey}" Color = "Red"/> 8 <! -- Highlight text Color --> 9 <SolidColorBrush x: Key = "{x: Static SystemColors. HighlightTextBrushKey}" Color = "Black"/> 10 <! -- Non-highlighted text color --> 11 <SolidColorBrush x: Key = "{x: Static SystemColors. controlTextBrushKey} "Color =" Green "/> 12 <Style TargetType =" ListBox "> 13 <! -- Redefines the style of items in ListBox --> 14 <Setter Property = "ItemTemplate"> 15 <Setter. value> 16 <DataTemplate> 17 <Grid Margin = "0" Width = "150"> 18 <Border Margin = "5" BorderBrush = "SteelBlue" BorderThickness = "1" CornerRadius = "5" Background = "{Binding Path = Background, relativeSource = {RelativeSource Mode = FindAncestor, AncestorType = {x: Type ListBoxItem }}"> 19 <Grid Margin = "5"> 20 <Grid. rowDefinitions> 21 <RowDefinition/> 22 <RowDe Finition/> 23 </Grid. rowDefinitions> 24 <TextBlock Text = "{Binding ID}"/> 25 <TextBlock Grid. row = "1" Text = "{Binding Name}"/> 26 </Grid> 27 </Border> 28 </Grid> 29 </DataTemplate> 30 </Setter. value> 31 </Setter> 32 <! -- Customize the color of the selected item --> 33 <Setter Property = "ItemContainerStyle"> 34 <Setter. value> 35 <Style TargetType = "ListBoxItem"> 36 <Style. triggers> 37 <Trigger Property = "ListBoxItem. isSelected "Value =" True "> 38 <Setter Property =" ListBoxItem. background "Value =" Green "/> 39 </Trigger> 40 </Style. triggers> 41 </Style> 42 </Setter. value> 43 </Setter> 44 </Style> 45 </ListBox. resources> 46 </ListBox>