title: c# wpf how to change color of a cell in a data grid if its value is out of range
body: <p>I want to change the color of a cell in a data grid if the value entered is out of range.Column to be edited is &quot;Value1&quot;. How best to do it?
  Thanks in advance.
  xaml:</p>
  <pre><code>&lt;TreeViewItem TreeViewItem.Expanded=&quot;TreeViewItem1_Expanded&quot; TreeViewItem.Collapsed=&quot; TreeViewItem1_Collapsed&quot; x:Name=&quot;Parametertreeviewitem1&quot; Header=&quot;Motor Data&quot; VerticalContentAlignment=&quot;Center&quot;   FontFamily=&quot;Segoe UI&quot; RenderTransformOrigin=&quot;0.5,0.5&quot;  FontSize=&quot;16&quot; Width=&quot;896&quot; HorizontalAlignment=&quot;Left&quot; HorizontalContentAlignment=&quot;Center&quot; &gt;
                          &lt;DataGrid x:Name=&quot;Data_grid1&quot; Height=&quot;520&quot; Margin=&quot;0,5,0,-25&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;1150&quot; AutoGenerateColumns=&quot;False&quot; SelectionMode=&quot;Single&quot; CellEditEnding=&quot;CellEditEnding&quot;&gt;
                              &lt;DataGrid.Columns&gt;
                                  &lt;DataGridTextColumn Header=&quot;Display&quot;  Width=&quot;100&quot; Binding=&quot;{Binding Display1}&quot; IsReadOnly=&quot;True&quot;/&gt;
                                  &lt;DataGridTextColumn Header=&quot;Parameter&quot; Width=&quot;250&quot; Binding=&quot;{Binding Parameter1}&quot; IsReadOnly=&quot;True&quot;/&gt;
                                  &lt;DataGridTextColumn Binding=&quot;{Binding Range1}&quot; Header=&quot;Range&quot; Width=&quot;200&quot; IsReadOnly=&quot;True&quot;/&gt;
                                  &lt;DataGridTextColumn Binding=&quot;{Binding Value1, ValidatesOnExceptions=True}&quot; Header=&quot;Drive value&quot; Width=&quot;150&quot;/&gt;
                                  &lt;DataGridTextColumn Binding=&quot;{Binding File_Value1}&quot; Header=&quot;File value&quot; Width=&quot;150&quot; IsReadOnly=&quot;True&quot;/&gt;
                              &lt;/DataGrid.Columns&gt;
                          &lt;/DataGrid&gt;
                      &lt;/TreeViewItem&gt;
  </code></pre>
  <p>cs:</p>
  <pre><code> private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
      {
          para_num = (byte)Data_grid.SelectedIndex;
          var editedTex = e.EditingElement as TextBox;
          if ((Current_grp[para_num].Min_value &lt;= (Convert.ToDecimal(editedTex.Text)) * Current_grp[para_num].Scale) &amp;&amp; (Convert.ToDecimal(editedTex.Text) * Current_grp[para_num].Scale &lt;= Current_grp[para_num].Max_value))
          {
           // This code runs if edit is valid.
           // above condition checks whether parameter is in proper range.
              
          }
  </code></pre>