title: how to change datagrid row background color with value taken from database in wpf
body: <p>I want to change datagrid row background color, by taking value from Database. 
  I have 2 value "ERROR" and "OK". If Column string value is ERROR then row color will be red and if OK then It must be Green. This value get from database by executing query. I have these values in dataset. I am not clear that how to achieve this?</p>
  
  <p>I have tried below code:</p>
  
  <pre><code>&lt;Window x:Class="stackDatagridColor.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:viewModels="clr-namespace:stackDatagridColor"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.Resources&gt;
      &lt;viewModels:viewmodel x:Key="viewmodel"/&gt;
      &lt;viewModels:BrushConverter x:Key="BrushConverter"/&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;StackPanel&gt;
          &lt;DataGrid ItemsSource="{Binding Collection, Mode=TwoWay, Source={StaticResource viewmodel}, UpdateSourceTrigger=PropertyChanged}"&gt;
              &lt;DataGrid.RowStyle&gt;
                  &lt;Style TargetType="DataGridRow"&gt;
                      &lt;Style.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Status}" Value="ERROR"&gt;
                              &lt;Setter Property="Background" Value="Red"&gt;&lt;/Setter&gt;
                          &lt;/DataTrigger&gt;
                          &lt;DataTrigger Binding="{Binding Status}" Value="OK"&gt;
                              &lt;Setter Property="Background" Value="Green"&gt;&lt;/Setter&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/DataGrid.RowStyle&gt;
          &lt;/DataGrid&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>ViewModel:</p>
  
  <pre><code>public class viewmodel : INotifyPropertyChanged
  {
  
      private ObservableCollection&lt;myItem&gt; collection;
      public ObservableCollection&lt;myItem&gt; Collection
      {
          get { return collection; }
          set { collection = value; OnPropertyChanged("Collection"); }
      }
  
  
      public viewmodel()
      {
          Collection = new ObservableCollection&lt;myItem&gt;();
          myItem item1 = new myItem { Name = "name1", Status = "OK" };
          myItem item2 = new myItem { Name = "name2", Status = "ERROR" };
          DispatchService.Invoke(() =&gt;
              {
                  Collection.Add(item1);
              Collection.Add(item2);
              });
      }
  
  
      #region INotifyPropertyChanged Members
  
      public event PropertyChangedEventHandler PropertyChanged = delegate { };
  
      protected void OnPropertyChanged(string propertyName)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>Simple Class</p>
  
  <pre><code>public class myItem
  {
      public string Status { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Dispatcher class </p>
  
  <pre><code>public static class DispatchService
  {
      public static void Invoke(Action action)
      {
          Dispatcher dispatchObject = Application.Current.Dispatcher;
          if (dispatchObject == null || dispatchObject.CheckAccess())
          {
              action();
          }
          else
          {
              dispatchObject.Invoke(action);
          }
      }
  }
  </code></pre>
  
  <p>Converter:</p>
  
  <pre><code>public class BrushConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          string input = value as string;
          switch (input)
          {
              case "ERROR":
                  return Brushes.Red;
              case "OK":
                  return Brushes.Green;
              default:
                  return DependencyProperty.UnsetValue;
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          throw new NotSupportedException();
      }
  }
  </code></pre>
  
  <p>I just want to bind dataset column to trigger. If trigger get ERROR string then background row color change to red and vice versa.</p>