WPF 里,ListBox 的 Item 被点击后 ,背景是 蓝色的。
有时候我们不想要蓝色,想要根据项目环境来决定颜色,可以像下面这样做。
<Style x:Key="lbiOri" TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush Color="LightGreen" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
<SolidColorBrush Color="Black" x:Key="{x:Static SystemColors.HighlightTextBrushKey}"/>
</Style.Resources>
</Style>
Style 的 TargetType 指定为 ListBoxItem。
SystemColors.HighlightBrushKey 指定绘制选定项的背景的对象为该 SolidColorBrush。
SystemColors.HighlightTextBrushKey 指定绘制选定项的文字的对象为该 SolidColorBrush。
接着,我们只需要指定 ItemContainerStyle 为定义的资源即可。
<ListBox ItemContainerStyle="{StaticResource lbiOri}"/>
ItemContainerStyle 指定了 Item 的 Panel。