title: Changing WPF Listbox SelectedItem text color and highlight/background Color using C#
body: <p>I am trying to change the highlighted(selected) color and the highlighted text color of a wpf listbox at runtime. I have tried creating a style and applying it as follows:</p>
  
  <pre><code>    Style s = new Style(typeof(ListBox));
      s.Resources.Add(SystemColors.HighlightBrushKey, Setting.ListSelectedColor);
      s.Resources.Add(SystemColors.HighlightTextBrushKey, Setting.ListSelectedTextColor);
      lstGames.Style = s;
  </code></pre>
  
  <p>But this seems to do nothing. Is there any way to achieve this?</p>
  
  <p>EDIT: </p>
  
  <p>Per suggestions, I tried using DynamicResources to achieve this, but so far this has not been successful either. My code for this:</p>
  
  <p>DYNAMICRESOURCES</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;Color x:Key="ListTextSelectedColor"/&gt;
      &lt;Color x:Key="ListSelectedColor"/&gt;
  &lt;/UserControl.Resources&gt;
  </code></pre>
  
  <p>LISTBOX</p>
  
  <pre><code>        &lt;ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
               Name="lstGames" Margin="20" Grid.Row="2" Grid.Column="2" 
               SelectionChanged="lstGames_SelectionChanged" Grid.RowSpan="2" Grid.ColumnSpan="2" 
               Background="{x:Null}" BorderBrush="{x:Null}" SelectionMode="Single"
               FontSize="18" FontFamily="OCR A Extended"&gt;
          &lt;Style TargetType="ListBox"&gt;
              &lt;Style.Resources&gt;
                  &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource ListSelectedColor}"/&gt;
                  &lt;SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource ListSelectedColor}"/&gt;
                  &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/&gt;
                  &lt;SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/&gt;
              &lt;/Style.Resources&gt;
          &lt;/Style&gt;
      &lt;/ListBox&gt;
  </code></pre>
  
  <p>APPLYING RESOURCES IN C#</p>
  
  <pre><code>this.Resources["ListSelectedColor"] = SETING.ListSelectedColor.Color;
  this.Resources["ListTextSelectedColor"] = SETTING.ListSelectedTextColor.Color;
  </code></pre>