title: C# WPF edit datagrid cell colour based on data length
body: <p>I have a datagrid and if the cell text is over 32 characters, i want to make the cell red.</p>
  
  <p>I've seen other solutions based on when the cell text is certain text but i'm not sure how to go about using those solutions here</p>
  
  <p>my XAML is as follows</p>
  
  <pre><code>&lt;DataGrid Grid.Row="2" Name="DataGridView1" CanUserSortColumns="False" CanUserReorderColumns="False" IsReadOnly="True" ItemsSource="{Binding}" Background="LightGray" RowBackground="#BDBDBF" AlternatingRowBackground="#E3E3E5"&gt;&lt;/DataGrid&gt;  
  </code></pre>
  
  <p>To put my data in i read it into a DataTable called dt and do as follows. </p>
  
  <pre><code>DataGridView1.DataContext = dt.DefaultView;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Using some code from Daniel W. i have got it to partially work, i just need the finishing touches now.</p>
  
  <p>I have got it to work for 1 column by doing the following:</p>
  
  <pre><code>  &lt;DataGrid  Grid.Row="2" Name="DataGridView1" CanUserSortColumns="False" CanUserReorderColumns="False" IsReadOnly="True" ItemsSource="{Binding}" Background="LightGray" RowBackground="#BDBDBF" AlternatingRowBackground="#E3E3E5"&gt;
              &lt;DataGrid.Columns&gt;
  
                  &lt;DataGridTextColumn Binding="{Binding Address1}"&gt;
                      &lt;DataGridTextColumn.ElementStyle&gt;
                          &lt;Style TargetType="{x:Type TextBlock}"&gt;
                              &lt;Setter Property="Background" Value="{Binding Address1, Converter={StaticResource brushConverter}}"&gt;
                              &lt;/Setter&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.ElementStyle&gt;
                  &lt;/DataGridTextColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;  
  </code></pre>
  
  <p>However this does mess up the formatting of my datatable</p>
  
  <p><a href="https://i.gyazo.com/525ce05a30cad36458a6734d6c61a0ff.png" rel="nofollow noreferrer">https://i.gyazo.com/525ce05a30cad36458a6734d6c61a0ff.png</a></p>
  
  <p>As you can see the 'address1' column i want editing is not edited, but rather a new column is made to the left of the regular columns.</p>
  
  <p>Now, i cant define each column as the column name can change at run time so i wont know what i'm binding it to</p>
  
  <p>I need a solution that will just act as a template as my columns aren't defined until i read in the csv which can have any number of columns / names so i cant do data binding to these columns</p>
  
  <p>Any ideas?</p>