views:

4381

answers:

2
+8  A: 

The ListBox template uses a system color called ControlBrush to set the inactive highlight color. Therefore, you can just override that color:

<ListBox>
    <ListBox.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush>
    </ListBox.Resources>
</ListBox>

HTH, Kent

Kent Boogaart
There issues with doing it this way. It doesn't allow you to skin the app, and if you go and change the system colors by changing your Windows Theme, it will revert back
Micah
Doesn't allow you to skin the app? How so? Just look up a resource for the color instead of hard-coding as red. And I just tested changing the theme with a test harness running, and it worked perfectly. Please clarify.
Kent Boogaart
That's suprising. What happens if you put other listboxes on a form that you don't want styled that way?
Micah
The resource is defined at the ListBox level, so it only affects that ListBox.
Kent Boogaart
What's actually happening is you are overriding the style key for all objects that use it.
Micah
It's a resource so subject to the normal rules of resource lookup. Micah, try it yourself. Put two ListBoxes in a Window - one with the resource override and one without. The color change only affects the one with the resource override.
Kent Boogaart
Thanks for the comments, both of you. I agree that there are potential issues with this solution (variable Windows Themes, etc.), but in this case I want the inactive highlighting to be the same color as the active highlighting, so this solution is exactly what I'm looking for.
Joseph Sturtevant
I have exactly same problem. But my font color needs to be white. At moment it is set to white, but it's gets override by this resource to black. Any ideas how to change that? Thank You.
Daniil Harik
+2  A: 

The answer will in some cases solve the problem, but is not ideal as it breaks when the control is disabled/readonly and it also overrides the color schemes, rather than taking advantage of them. My suggestion is to add the following in the ListBox tags instead:

<ListBox....>
    <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="Border" Property="Background"
                                            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
        </Style>
    </ListBox.Resources>
</ListBox>

What this will do is set the Highlight background color on the list box item whenever it is selected (regardless of the control state).

My answer is based on help from the answer already given, along with the following blog: http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx

Thies