问题描述
我一直在寻找如何更改失去焦点的列表框中所选项目的文本颜色.
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如何在列表框失去焦点时更改列表框所选项目的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!