更新时间:2021-10-26 01:00:09
可以做的简单很多.所选 ListBox 项的背景颜色取自 SystemColors.因此,您需要做的是覆盖 ListBox 资源中的 SystemColors:
It can be done a lot simpler. The Background color for the selected ListBox items are taken from the SystemColors. So, what you need to do is override the SystemColors in the Resources of your ListBox:
<ListBox.Resources>
<!--Selected color when the ListBox is focused-->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
<!--Selected color when the ListBox is not focused-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
</ListBox.Resources>