title: Dynamically change the Border Background by setting property in xaml
body: <p>I'm trying to dynamically change a Border Background property named "Brush2", but I get the following error.</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Windows.Media.Color' to type
    'System.Windows.Media.Brush'.</p>
  </blockquote>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;Color x:Key="Brush0"&gt;#7F605F5F&lt;/Color&gt;
      &lt;Color x:Key="Brush1"&gt;#7F7E7E7E&lt;/Color&gt;
  
      &lt;Color x:Key="Color2"&gt;#7FFDFDFD&lt;/Color&gt;
      &lt;SolidColorBrush  x:Key="Brush2" Color="{DynamicResource Color2}" /&gt;
  
      &lt;Color x:Key="Brush3"&gt;#7FFFFFFF&lt;/Color&gt;
      &lt;Color x:Key="Brush4"&gt;#BFFFFFFF&lt;/Color&gt;
      &lt;Color x:Key="Brush5"&gt;#F2FFFFFF&lt;/Color&gt;
      &lt;Color x:Key="Brush6"&gt;#FF6096E5&lt;/Color&gt;
      &lt;Color x:Key="Brush7"&gt;#FF9B7C35&lt;/Color&gt;
      &lt;Color x:Key="Brush8"&gt;#FFB0BAA1&lt;/Color&gt;
      &lt;Color x:Key="Brush9"&gt;#7F60605F&lt;/Color&gt;
      &lt;Color x:Key="Brush10"&gt;White&lt;/Color&gt;
  
  
  
      &lt;Style TargetType="{x:Type TabItem}" &gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type TabItem}"&gt;
                      &lt;Grid&gt;
                          &lt;VisualStateManager.VisualStateGroups&gt;
  
  
                              &lt;VisualStateGroup x:Name="CommonStates"&gt;
                                  &lt;!-- silver --&gt;
                                  &lt;VisualState x:Name="Selected"&gt;
                                      &lt;Storyboard&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush8}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                      &lt;/Storyboard&gt;
                                  &lt;/VisualState&gt;
  
  
                                  &lt;VisualState x:Name="Normal"/&gt;
  
  
                                  &lt;VisualState x:Name="MouseOver"&gt;
                                      &lt;!-- bronze --&gt;
                                      &lt;Storyboard&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush7}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush0}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                          &lt;ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"&gt;
                                              &lt;EasingColorKeyFrame KeyTime="0" Value="{DynamicResource Brush9}"/&gt;
                                          &lt;/ColorAnimationUsingKeyFrames&gt;
                                      &lt;/Storyboard&gt;
                                  &lt;/VisualState&gt;
  
  
  
                                  &lt;VisualState x:Name="Flashing"&gt;
                                      &lt;Storyboard&gt;
                                          &lt;DoubleAnimation 
                                              Storyboard.TargetName="BorderGlass"
                                              Storyboard.TargetProperty="Opacity"
                                              From="0.0"
                                              To="1.0"                     
                                              Duration="0:0:0.33"
                                              AutoReverse="True"
                                              RepeatBehavior="Forever"/&gt;
                                      &lt;/Storyboard&gt;
                                  &lt;/VisualState&gt;
  
  
                                  &lt;VisualState x:Name="Disabled"/&gt;
                              &lt;/VisualStateGroup&gt;
  
  
                          &lt;/VisualStateManager.VisualStateGroups&gt;
  
                          &lt;Border x:Name="border" BorderThickness="3" CornerRadius="5,5,0,0"&gt;
                              &lt;Border.BorderBrush&gt;
                                  &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush0}" Offset="0"/&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush1}" Offset="1"/&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Border.BorderBrush&gt;
                              &lt;Border.Background&gt;
                                  &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush6}" Offset="0.004"/&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush6}" Offset="0.823"/&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush10}" Offset="0.228"/&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Border.Background&gt;
                          &lt;/Border&gt;
                          &lt;Border x:Name="BorderGlass" BorderThickness="3" CornerRadius="5,5,0,0" Background="{DynamicResource Brush2}"&gt;
                              &lt;Border.BorderBrush&gt;
                                  &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush3}" Offset="0"/&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush4}" Offset="0.401"/&gt;
                                      &lt;GradientStop Color="{DynamicResource Brush5}" Offset="0.254"/&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Border.BorderBrush&gt;
                          &lt;/Border&gt;
  
                          &lt;ContentPresenter HorizontalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" ContentSource="Header"/&gt;
  
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
  
  
  &lt;/ResourceDictionary&gt;
  </code></pre>