WPF如何在列表框失去焦点时更改列表框选定项的文本颜色 [英] WPF How to change the listbox selected item text color when the list box loses focus
问题描述
我一直在寻找如何更改失去焦点的列表框中所选项目的文本颜色.
I've been searching for how to change the text color of a selected item in a list box that has lost focus.
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Orange"/>
这三个标签负责大部分工作,但我的列表框有黑色背景,当控件失去焦点时,字体变为黑色.
These three tags take care of most of the work, but my list box has a black background and when the control loses focus, the font turns to black.
我从另一篇文章 SystemColor 中找到了这个列表.从这个列表中提供大量可能选项的键 以及任何看似远程直观的东西都不起作用.有人知道我需要更改的密钥吗?
I found this list from another post SystemColor. Keys that gives a ton of possible options from this list and anything that seems remotely intuitive has not worked. Does anybody know the key that I need to change?
推荐答案
我把它放在资源字典中,用于包含列表框的元素:
I put this in a resource dictionary for an element containing the listbox:
<Style TargetType="ListBoxItem">
<Style.Resources>
<!--SelectedItem with focus-->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
<!--SelectedItem without focus-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="White"/>
</Style.Resources>
</Style>
另请注意,在 .Net 4.5 中,您必须通过设置来询问旧"行为
Notice also that in .Net 4.5 you have to ask for "old" behavior by setting
FrameworkCompatibilityPreferences.
AreInactiveSelectionHighlightBrushKeysSupported = false;
在创建任何窗口之前的程序早期.
early in your program before any windows are created.
这篇关于WPF如何在列表框失去焦点时更改列表框选定项的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!