Setting Background Color or WPF (4.0) ListBox - Windows 8

Solution 1:

Those posts are getting outdated for Windows-8.

In Windows-8 for some reason Microsoft don't want people editing their Default Style's so easily or something with a Brush over-ride.

ListBoxItem default Style from VS has this for selection triggers:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="False" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="#3DDADADA" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="#FFDADADA" />
</MultiTrigger>
<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="True" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="#3D26A0DA" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="#FF26A0DA" />
</MultiTrigger>

Triggers for Selection state no longer are applying brushes we can over-ride easily but are static colors. Hence to modify it you are going to need to derive the template and modify the trigger there. to White

This is the full Style given by VS2012 Windows-8 for ListBoxItem

<Style x:Key="ListBoxItemStyle1"
       TargetType="{x:Type ListBoxItem}">
  <Setter Property="SnapsToDevicePixels"
          Value="True" />
  <Setter Property="Padding"
          Value="4,1" />
  <Setter Property="HorizontalContentAlignment"
          Value="{Binding HorizontalContentAlignment,
                          RelativeSource={RelativeSource FindAncestor,
                                                         AncestorLevel=1,
                                                         AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="VerticalContentAlignment"
          Value="{Binding VerticalContentAlignment,
                          RelativeSource={RelativeSource FindAncestor,
                                                         AncestorLevel=1,
                                                         AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="Background"
          Value="Transparent" />
  <Setter Property="BorderBrush"
          Value="Transparent" />
  <Setter Property="BorderThickness"
          Value="1" />
  <Setter Property="FocusVisualStyle">
    <Setter.Value>
      <Style>
        <Setter Property="Control.Template">
          <Setter.Value>
            <ControlTemplate>
              <Rectangle Margin="2"
                         SnapsToDevicePixels="True"
                         Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                         StrokeDashArray="1 2"
                         StrokeThickness="1" />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Bd"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}"
                SnapsToDevicePixels="True">
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            Content="{TemplateBinding Content}"
                            ContentStringFormat="{TemplateBinding ContentStringFormat}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>
        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsMouseOver"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#1F26A0DA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#A826A0DA" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                         Value="False" />
              <Condition Property="IsSelected"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#3DDADADA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#FFDADADA" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                         Value="True" />
              <Condition Property="IsSelected"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#3D26A0DA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#FF26A0DA" />
          </MultiTrigger>
          <Trigger Property="IsEnabled"
                   Value="False">
            <Setter TargetName="Bd"
                    Property="TextElement.Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

if you modify those triggers to:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="False" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="White" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="White" />
</MultiTrigger>
<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="True" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="White" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="White" />
</MultiTrigger>

you should have your issue sorted.

Solution 2:

Adding the following trigger to my Item DataTemplate, worked for Windows 10:

<DataTemplate x:Key="MyItemTemplate">
    <Border Name="Border" Background="Transparent" BorderBrush="LightGray" BorderThickness="0,1,0,0" Padding="0">
        <TextBlock Text="{Binding Text}" HorizontalAlignment="Left" FontWeight="Medium" />
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
            <Setter TargetName="Border" Property="Background" Value="SkyBlue"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>