title: Passing through StaticResource style defined in XAML in C# codebehind
body: <p>I am building a WPF application. XAML used for front end and C# for code behind</p>
  
  <p>I have the following section of code that generates my XAML for me dynamically.</p>
  
  <pre><code>if (station_item.Checker_Setup.First().Checker_Log.OrderByDescending(log =&gt; log.date).First().Status.status_key == 2)
                      {
                          Path path = new Path();
                          path.Data = new RectangleGeometry(new Rect(0, 0, 19, 21), 3, 3);
                          path.Style = "{StaticResource statusIndicatorRed}";
                          TextBlock block = new TextBlock();
                          block.Text = station_item.station_name;
                          WrapBox.Children.Add(path);
                          WrapBox.Children.Add(block);
                      } 
  </code></pre>
  
  <p>However where I have </p>
  
  <pre><code>path.Style = "{StaticResource statusIndicatorRed}";
  </code></pre>
  
  <p>I get the following error</p>
  
  <p><em>Cannot implicitly convert type String  to System.Windows.Style</em></p>
  
  <p>The style is defined in my MainWindow.xaml as follows</p>
  
  <pre><code>&lt;Style x:Key="statusIndicatorRed" TargetType="Path"&gt;
          &lt;Setter Property="Fill" Value="#B2203D" /&gt;
          &lt;Setter Property="Width" Value="19px" /&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Left" /&gt;
          &lt;Setter Property="ToolTipService.ShowDuration" Value="30000" /&gt;
          &lt;Setter Property="Cursor" Value="Help" /&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>How would I pass this style through in my code behind? Is this even a good way to do things? </p>