I'm fairly new to WPF but experienced in .NET (Winforms). I'm trying to manipulate the highlight style of a listbox to control the focused and unfocused color of the selected item. Every single tutorial on this that I have found uses a custom style to assign a new value to the SystemColors.HighlightBrushKey and SystemColors.ControlBrushKey. But it isn't working. After countless hours trying to get this to work, it occurred to me that maybe it was OS related. I had been trying it on a Windows 10 system. I ran the exact same code on a Windows 7 setup, and lo and behold, it worked!
So apparently the old method doesn't work in Windows 10 (at least that's what it looks like to me). Has anybody found an alternative? At the end of the day, I just want the listbox to maintain the bright highlight even when it doesn't have focus. The default grey highlight is difficult to see, and doesn't seem appropriate in some usages. I have a real world scenario where it feels very unnatural for the highlight to basically disappear when the focus moves away from the ListBox.
Below is the XAML code I used that worked on Windows 7 but not on Windows 10. (By the way, I have also tried replacing SystemColors.ControlBrushKey with SystemColors.InactiveSelectionHighlightBrushKey -- the results were the same).
<Window x:Class="TestApp.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestApp"
mc:Ignorable="d"
Title="TestWindow" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="myListboxStyle">
<Style.Resources>
<!-- Background of selected item when focused -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
<!-- Background of selected item when not focused -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<ListBox x:Name="listBox" Style="{StaticResource myListboxStyle}" HorizontalAlignment="Left" Height="100" Margin="22,18,0,0" VerticalAlignment="Top" Width="237">
<ListBoxItem>Test 1</ListBoxItem>
<ListBoxItem>Test 2</ListBoxItem>
<ListBoxItem>Test 3</ListBoxItem>
</ListBox>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="50,165,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
I had a similar issue with the highlight style of ListBoxes under Windows 10...again the old SystemColors.HighlightBrushKey and SystemColors.ControlBrushKey solution only worked for me under Windows 7. In my case, I simply wanted to remove any WPF-drawn highlighting at all, so I could replace it with my own.
For this, I simply replaced the ControlTemplate via the following brief style in my Resources:
I generally dislike replacing verbose ControlTemplates, but this was a nice terse solution for me, hoping it might help somebody else.
If you really want to change the defaults for your app, you can always modify the style by making a copy of the template. In this case ListBoxItem style.
In the designer, click on a ListBoxItem, right click Edit Template, and Edit a Copy.
Below is what I got on my machine, and a screenshot of the app running with Red/Green demonstrating the style applied to affected items. You would of course apply this to all items...
And the app in action:
ACTIVE:
INACTIVE:
Caveat emptor.
I'm afraid that I don't have a way to test this in Windows 10 specifically, but I ran into a similar problem just on Windows 7. Defining the "ControlBrushKey" in the Resources just wasn't working.
Setting the "InactiveSelectionHighlightBrushKey" worked for me:
Dodger blue is a very close match to the default Aero selection; it's just a hair darker. I'm still working on figuring out how to map it to the actual selection value rather than a specific color.
Microsoft broke this for windows 10, but we can fix it!
Here is what the template looks like in windows 10 (just the parts i care about):
Notice they have hard-coded the values for the colors, like "#1F26A0DA".
In windows 7, the built-in template for ListBoxItems was:
So basically Microsoft was using resources like "SystemColors.InactiveSelectionHighlightBrushKey" in windows 7. But now they made it so we can't do this without overriding the template; since they hard-coded in all the values.
So to patch this, simply override the Template for ListBoxItem in you App.Xaml file; so that everything gets the patch.