I'm trying to hide the visual hints that indicate selection in a WPF ListBox
. This answer suggests this should work by overriding the SystemColors
for that ListBox
.
I created a new WPF project and edited the MainWindow.xaml
like this:
<Window x:Class="WpfListboxWithoutSelection.MainWindow"
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:WpfListboxWithoutSelection"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="325">
<Grid>
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
<s:String>Item 1</s:String>
<s:String>Item 2</s:String>
<s:String>Item 3</s:String>
</ListBox>
</Grid>
</Window>
Unfortunately this doesn't work, the window appears like this:
Any idea what I'm doing wrong? How can I remove the blue colors that appear on the selected item and on the one hovered?
If you wish to disable the highlighting when a listboxitem is selected or mouse over, you can use the below code.
the only way I have found to override this default behavior is to override the default style for ListBoxItem. I extract the default style from blend and override it for your needs. Here is how it's done in my case:
And you can still select the items:
Hope this helps.
definitely it wont work. Since ListBox.Resources will set a resource for ListBox not for ListBoxItem. So here is a solution:
Attach this style in windows.resource like:
And defiantly you can put more SolidColorBrush into Style.Resources like what you did in your code.