title: Removing Duplicate DataTrigger Code from XAML
body: <p>I am creating a lot of DataGrids in my WPF Application in the XAML file. </p>
  
  <p>Generally, each DataGrid I make look like this:</p>
  
  <pre><code>&lt;DataGrid x:Name="DamageTakenData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="172" Canvas.Left="30" Canvas.Top="228"&gt;
                  &lt;DataGrid.CellStyle&gt;
                      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
                          &lt;Style.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="1"&gt;
                                  &lt;Setter Property="Foreground" Value="Red" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="2"&gt;
                                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="3"&gt;
                                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="4"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="5"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="6"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/DataGrid.CellStyle&gt;
           &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I have about 6 or 7 of those DataGrids, each of them in a different tab. Each DataGrid has the same DataGrid.CellStyle</p>
  
  <pre><code>&lt;DataGrid x:Name="PlayerDeathData" HorizontalAlignment="Left" Margin="27,35,0,0" VerticalAlignment="Top" Height="155" Width="626" Grid.ColumnSpan="2"&gt;
                  &lt;DataGrid.CellStyle&gt;
                      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
                          &lt;Style.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="1"&gt;
                                  &lt;Setter Property="Foreground" Value="Red" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="2"&gt;
                                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="3"&gt;
                                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="4"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="5"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding Row}" Value="6"&gt;
                                  &lt;Setter Property="Foreground" Value="Green" /&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/DataGrid.CellStyle&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>How would I go about eliminating the duplicate code for specifying DataGrid.CellStyle?</p>
  
  <p><strong>Attempts</strong></p>
  
  <p>Looking at this question: <a href="https://stackoverflow.com/questions/28237844/prevent-duplicate-code-in-xaml">Prevent Duplicate Code In XAML</a>, I've tried something similar. I made a static resource:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;Style TargetType="{x:Type DataGridCell}" x:Key="DataGridStyle"&gt;
          &lt;Style.Triggers&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="1"&gt;
                  &lt;Setter Property="Foreground" Value="Red" /&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="2"&gt;
                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="3"&gt;
                  &lt;Setter Property="Foreground" Value="Orange" /&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="4"&gt;
                  &lt;Setter Property="Foreground" Value="Green" /&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="5"&gt;
                  &lt;Setter Property="Foreground" Value="Green" /&gt;
              &lt;/DataTrigger&gt;
              &lt;DataTrigger Binding="{Binding Row}" Value="6"&gt;
                  &lt;Setter Property="Foreground" Value="Green" /&gt;
              &lt;/DataTrigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>And then, I use the static resource like this:</p>
  
  <pre><code>&lt;DataGrid x:Name="DamageDealtData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="163" Canvas.Left="30" Canvas.Top="35"&gt;
                  &lt;DataGridCell Style="{StaticResource DataGridStyle}"/&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>This approach doesn't work either. Because it seems to be creating only 1 DataGridCell and styling it. </p>
  
  <p>How would I go about doing that?</p>