【WPF】WPF ListBox,TreeView的SelectedItem失去焦点的颜色
<ListBox> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> /*这一段是关键*/ <Style.Resources> <!--SelectedItem with focus--> <!--<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="AliceBlue"/>--> <!--SelectedItem without focus--> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightSkyBlue" /> </Style.Resources> <Setter Property="Padding" Value="8"></Setter> <Setter Property="VerticalContentAlignment" Value="Center"></Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="LightSkyBlue" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox>
上面无效的话,使用下方的代码试试:(针对的是treeview,当然listbox也通用)
<TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/> </Style.Resources> </Style> </TreeView.ItemContainerStyle>
更新:2021-08-29
我也是刚发现的问题,windows10系统貌似上述2个方法都不行,让人抓狂。
最后百度总算找到解决方法:原文:https://blog.csdn.net/qq_40841733/article/details/90778121
<Style x:Key="FileListBoxItem" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Yellow"/> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="LightSkyBlue"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsSelected" Value="true"/> <Condition Property="Selector.IsSelectionActive" Value="false"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="LightSkyBlue"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> </MultiTrigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>