title: XAML to change background opacity without changing color
body: <p>In my <em>C#</em> / <em>WPF</em> / <em>.NET 4.5</em> app I would like to implement a <code>Trigger</code> that toggles the background opacity of a control without changing its color.</p>
  
  <p>Normally to set the background brush of a control, I would use the following <code>Setter</code>:</p>
  
  <pre><code>&lt;Setter TargetName="TargetControl" Property="Background"&gt;
  &lt;Setter.Value&gt;
    &lt;SolidColorBrush Color="Red" Opacity="0.2"/&gt;
  &lt;/Setter.Value&gt;
  </code></pre>
  
  <p>Now I am in a situation where I have to change the opacity only and not the color. What would my setter look like in this case, where I don't care what color the background is but I want to preserve the color while changing the opacity?</p>
  
  <h3>More details:</h3>
  
  <p>The reason for this is that I am working on an <code>ItemsControl</code> where the background color changes depending on the item's <code>AlternationIndex</code>:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding SomeCollection}" AlternationCount="6"&gt;
    &lt;ItemsControl.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;Grid x:Name="GridWithin"&gt;
        &lt;!-- ... --&gt;
        &lt;/Grid&gt;
        &lt;DataTemplate.Triggers&gt;
          &lt;!-- Aternating colors for each row --&gt;
          &lt;Trigger Property="ItemsControl.AlternationIndex" Value="0"&gt;
            &lt;Setter TargetName="GridWithin" Property="Background"&gt;
              &lt;Setter.Value&gt;
                &lt;SolidColorBrush Color="Red" Opacity="0.2"/&gt;
              &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="ItemsControl.AlternationIndex" Value="1"&gt;
            &lt;Setter TargetName="AllGesturesDataTemplateGrid" Property="Background"&gt;
              &lt;Setter.Value&gt;
                &lt;SolidColorBrush Color="Green" Opacity="0.2"/&gt;
              &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
          &lt;/Trigger&gt;
          &lt;!-- ... --&gt;
          &lt;!-- Highlight when a hit is registered --&gt;
          &lt;DataTrigger&gt;
            &lt;DataTrigger.Conditions&gt;
              &lt;Condition Binding="{Binding IsHit}" Value="True" /&gt;
            &lt;/MultiDataTrigger.Conditions&gt;
            &lt;!-- The culprit --&gt;
            &lt;Setter TargetName="GridWithin" Property="Background"&gt;
              &lt;Setter.Value&gt;
                &lt;SolidColorBrush Opacity="0.7"/&gt;
              &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
            &lt;!-- /culprit --&gt;
          &lt;/DataTrigger&gt;
        &lt;/DataTemplate.Triggers&gt;
      &lt;/DataTemplate&gt;
    &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>