Search code examples
c#wpfxamllistviewitem

ListViewItem Background Not Changing Despite Setting Global ItemContainerStyle in App.xaml


I'm working on a WPF application where I want to apply a global style to all ListViewItem elements across my application. I've defined an ItemContainerStyle for ListViewItem in App.xaml, intending to change the background color of the selected item, hovered item, and default state. However, despite the global style being applied, the selected item background color remains unchanged (white), and it seems like the style is being overridden or ignored.

What I've Tried:

  1. Global Style in App.xaml: I've added the following ItemContainerStyle in App.xaml:
    <Application.Resources>
    <!-- Global ListViewItem Style -->
    <Style TargetType="ListViewItem">
        <Setter Property="Background" Value="#2B2B2B"/> <!-- Dark Background for Items -->
        <Setter Property="Foreground" Value="White"/> <!-- White Text for Items -->
        <Setter Property="FontFamily" Value="Century Gothic"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Padding" Value="5,2"/>

        <!-- Triggers for different states -->
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#FF4500"/> <!-- Orange on Hover -->
                <Setter Property="Foreground" Value="Black"/> <!-- Black Text on Hover -->
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#1E90FF"/> <!-- Custom Blue for Selected Line -->
                <Setter Property="Foreground" Value="White"/> <!-- White Text on Selection -->
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#555555"/> <!-- Gray Background when Disabled -->
                <Setter Property="Foreground" Value="#AAAAAA"/> <!-- Gray Text when Disabled -->
            </Trigger>
        </Style.Triggers>
    </Style>
</Application.Resources>
  1. Testing in Isolation:

    • I created a simple ListView in a separate window to test this style, but the selected background color is still not applying as expected.

    • I've tried setting the style directly in the ListView itself with similar results.

  2. Possible Conflicts:

    • I suspect there might be another style, theme, or third-party library (like MaterialDesignInXAML or MahApps.Metro) that might be overriding my style, but I haven't identified any specific conflicts.

    • I've checked for any global themes or other ResourceDictionary files that might be interfering, but the issue persists.

Environment:

  • .NET Version: 4.8

Question:

What could be causing the ItemContainerStyle in App.xaml to be ignored or overridden for ListViewItem? How can I ensure that my custom styles for selected, hovered, and default states are applied globally to all ListView controls?

Additional Context:

  • I’m looking for a solution that will allow me to manage this style globally rather than applying it locally to each ListView.

  • Any advice on diagnosing potential style conflicts or understanding why my style isn't applying would be greatly appreciated.


Solution

  • The ListView and ListViewItem ControlTemplates use system Color resources. For these resources, you need create overrides, which are "local" resources with the same key. Some resources that may apply to your situation include:

    <Color x:Key="DisabledControlLightColor">#FFE8EDF9</Color>
    <Color x:Key="DisabledControlDarkColor">#FFC5CBF9</Color>
    <Color x:Key="DisabledForegroundColor">#FF888888</Color>
    
    <Color x:Key="SelectedBackgroundColor">#FFC5CBF9</Color>
    <Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color>
    

    https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/listview-styles-and-templates?view=netframeworkdesktop-4.8