WPF How to change the listbox selected item text color when the list box loses focus

∥☆過路亽.° 提交于 2019-12-05 00:09:33

问题


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.

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?


回答1:


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>

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.




回答2:


use following code and just change the colors for example by using Colors.Black

listBox.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(Color.FromArgb(0xFF, 0x7F, 0xDB, 0x14)));
listBox.Resources.Add(SystemColors.ControlTextBrushKey, Brushes.White);

Good luck.



来源:https://stackoverflow.com/questions/3111763/wpf-how-to-change-the-listbox-selected-item-text-color-when-the-list-box-loses-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!