title: How to change WPF Rectangle Style Programatically
body: <p><strong>[EDIT]</strong> *I had forgotten to include the Exception returned </p>
  
  <p><code>'System.Windows.Style' is not a valid value for property 'Fill'</code>*</p>
  
  <p>This is my first post, I've already done lots of searching for questions where this might have already been answered to no success. I'm a complete noob when it comes to MVVM, Prism, and WPF and I have been thrown in the deep end on this one. </p>
  
  <p>I wish to change the contents of 2 rectangles depending on a boolean in the Model (Binding Passed). </p>
  
  <p>if a test returns Pass (bool True) then we display colour Green for first rectangle, and the second rectangle will display the UserControl.Resource Style x:Key="RectanglePass".
  If test fails, then first rectangle is red, and second displays UserControl.Resource Style x:Key="RectangleFail"</p>
  
  <p>I have the following xaml code:</p>
  
  <pre><code>&lt;UserControl x:Class="Integration.Memjet.Aoqc.Views.ProcessResultView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
  
   &lt;UserControl.Resources&gt;
      &lt;Style x:Key="RectangleFail" TargetType="Rectangle"&gt;
          &lt;Setter Property="Fill"&gt;
              &lt;Setter.Value&gt;
                  &lt;VisualBrush&gt;
                      ....
                  &lt;/VisualBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="RectanglePass" TargetType="Rectangle"&gt;
          &lt;Setter Property="Fill"&gt;
              &lt;Setter.Value&gt;
                  &lt;VisualBrush&gt;
                      ....
                  &lt;/VisualBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="60"&gt;&lt;/RowDefinition&gt;
          &lt;RowDefinition Height="60"&gt;&lt;/RowDefinition&gt;
          &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Label VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"&gt;PASS&lt;/Label&gt;
      &lt;Rectangle Name="rectStatus" Grid.Row="1"&gt;
          &lt;Rectangle.Style&gt;
              &lt;Style TargetType="{x:Type Rectangle}"&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding Passed}" Value="True"&gt;
                          &lt;Setter Property="Fill" Value="Green" /&gt;
                      &lt;/DataTrigger&gt;
                      &lt;DataTrigger Binding="{Binding Passed}" Value="False"&gt;
                          &lt;Setter Property="Fill" Value="Red" /&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/Rectangle.Style&gt;
      &lt;/Rectangle&gt;
  
      &lt;Rectangle Name="rectStatusImg" Width="120" Height="120" Grid.Row="2" &gt;
          &lt;Rectangle.Style&gt;
              &lt;Style TargetType="{x:Type Rectangle}"&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding Passed}" Value="True"&gt;
                          &lt;Setter Property="Fill" Value="{StaticResource RectanglePass}" /&gt;
                      &lt;/DataTrigger&gt;
                      &lt;DataTrigger Binding="{Binding Passed}" Value="False"&gt;
                          &lt;Setter Property="Fill" Value="{StaticResource RectangleFail}" /&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/Rectangle.Style&gt;
      &lt;/Rectangle&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The above doesn't work and returns an exception. 
  If I was to replace the second Rectangle tag (rectStatusImg) with this line;</p>
  
  <pre><code>&lt;Rectangle Name="rectStatusImg" Style="{StaticResource RectanglePass}" Width="120" Height="120" Grid.Row="2" &gt;&lt;/Rectangle&gt;
  </code></pre>
  
  <p>the xaml works and produces the expected result! But I wish to bind it to the Boolean Passed so I can change it programmatically. </p>
  
  <p>I really hope I was able to explain my problem here.</p>
  
  <p>Thank you for your help in advance, this site has always been a treasure and a great help. </p>
  
  <p>Cheers,
  Simon</p>