WPF Tips: Specify background color of the selected item in listbox
Posted Jane&Coding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Tips: Specify background color of the selected item in listbox相关的知识,希望对你有一定的参考价值。
当没有focus在listbox时,selectedItem的background为灰色,只有鼠标点击之后才会变成蓝色。希望即使鼠标没有点击的时候也是蓝色。
解决方法:
在xaml中添加:
<ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/> </ListBox.Resources>
或在app.xaml中添加:
<Style TargetType="ListBoxItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightBlue"/> </Style.Resources> <!--<Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="LightBlue" /> </Trigger> </Style.Triggers>--> </Style>
参考:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/38b88ef4-b4d1-4a61-a00e-e4f960896a9b/wpf-how-to-make-the-selection-item-highlight-color-stay-in-the-listbox-even-the-listbox-control?forum=wpf
https://blogs.msdn.microsoft.com/wpfsdk/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox/
以上是关于WPF Tips: Specify background color of the selected item in listbox的主要内容,如果未能解决你的问题,请参考以下文章