title: WPF Xaml Custom Styling Selected Item Style in a ListBox
body: <p>I have a <code>ListBox</code> that scrolls images horizontally.</p>
  
  <p>I have the following XAML I used blend to create it. It originally had a x:Key on the <code>Style TaregetType</code> line, MSDN said to remove it, as I was getting errors on that. Now I'm getting this error:</p>
  
  <p><code>Error  3   Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.</code></p>
  
  <p>I don't understand how to apply all of this junk that way, I've tried several things, nothing is working.</p>
  
  <p>My goal is to have the selected item's background be white, not blue.  Seems like a lot of work for something so small!</p>
  
  <p>Thanks.</p>
  
  <pre><code>    &lt;ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}"
          Grid.Row="1" Margin="2,26,2,104" ScrollViewer.VerticalScrollBarVisibility="Hidden"
               ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single" 
          x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}"  &gt;
          &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
              &lt;Setter Property="Background" Value="Transparent"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
              &lt;Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt;
              &lt;Setter Property="Padding" Value="2,0,0,0"/&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                          &lt;Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"&gt;
                              &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                          &lt;/Border&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="IsSelected" Value="true"&gt;
                                  &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="#FFFFFFFF"/&gt;
                              &lt;/Trigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="true"/&gt;
                                      &lt;Condition Property="Selector.IsSelectionActive" Value="false"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt;
                                  &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                  &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
          &lt;ListBox.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;VirtualizingStackPanel 
             Orientation="Horizontal" 
             IsItemsHost="True" /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ListBox.ItemsPanel&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Image Source="{Binding Image}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  </code></pre>