Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List/Combo Box Background And Selected Colours Under .net 4.5

I have an application that's been running happily on windows 7 and below targeting the .net 4 framework.
If the application is now installed in windows 8 (Running .net 4.5 but still targeting .net 4) it shows a blue background for a selected item in a listbox or combobox and a white background for a focused item. Is there anyway to remove this?
I'm using the following in my XAML to set the style in question which seemed to resolve the issue before windows 8.

<ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                    </Style.Resources>
                </Style>
            </ListBox.ItemContainerStyle>
like image 987
Oli Avatar asked Aug 17 '12 14:08

Oli


3 Answers

I forgot to come back with how I solved this.... Turns out that all you need to do it create a blank Item Container Style and assign it to your listbox/combobox etc.... You can use this as well as keeping the current style you may be using such as ListBox Style="{StaticResource CurrentStyle}" ItemContainerStyle="{StaticResource BlankListBoxContainerStyle}" /> Where BlankListBoxContainerStyle would be something like.....

<Style x:Key="BlankListBoxContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="FocusVisualStyle"
            Value="{x:Null}"/>
</Style>
like image 126
Oli Avatar answered Nov 16 '22 02:11

Oli


Your application overrides the value of SystemColors.HighlightBrushKey (and other system keys). This works for controls that define their foreground/background colors by referring to system colors, as they do in the Win7 default theme (Aero).
But Win8 default theme (Aero2) defines colors differently. So your override has no effect.

Themes are not required to use system colors. They happened to do so in Win7/Aero, but only because the system colors were deemed adequate.

Hope this helps.

like image 28
Anand Avatar answered Nov 16 '22 02:11

Anand


Rereading the documentation for frameworkcompatibility I found that it shouldn't actually be needed when compiling for .NET 4 and running on Windows 8.

http://msdn.microsoft.com/en-us/library/system.windows.frameworkcompatibilitypreferences%28v=vs.110%29.aspx

But I was still having the blue background that wasn't there in Windows 7 and I finally tracked the problem down to a list view which was not styled the same way as my list boxes. I found that I didn't actually need the ability to select elements for the elements, so changing the list view to a itemscontrol solved the problem for me.

See also:

http://connect.microsoft.com/VisualStudio/feedback/details/750655/selected-listboxitem-does-not-have-a-background-of-controlbrushkey-when-app-is-unfocused

like image 28
Anders Rune Jensen Avatar answered Nov 16 '22 02:11

Anders Rune Jensen