title: C# WPF DataTemplate set background color on property
body: <p>I have a TreeView containing a bunch of these Verify classes. Ultimately I would like to change the background color of an item to Green if the class's Success property is true and to Red if the property is false.</p>
  
  <pre><code>public class Verify : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      public string Name { get { return "Dummy Text"; } }
  
      private bool success;
      public bool Success
      {
          get { return success; }
          set { success = value; NotifyPropertyChanged(); }
      }
  
      public Verify()
      {
          Success = true; /* Test that the background changes color */
      }
  }
  </code></pre>
  
  <p>Here is where I've gotten so far in my TreeView. To test I've created an <code>ObservableCollection&lt;Verify&gt; VerifyWrite</code> and added a couple entries to it. This is what my TreeView is being bound to. I expect all the entries in my TreeView to be Green because I've set the Success to true but the background is not set to anything.</p>
  
  <pre><code>        &lt;TreeViewItem Header="Verify Write" IsExpanded="True" ItemsSource="{Binding VerifyWrite}"&gt;
              &lt;TreeViewItem.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Text="{Binding Name}"/&gt;
  
                      &lt;DataTemplate.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Success}" Value="True"&gt;
                              &lt;Setter Property="TreeViewItem.Background" Value="Green"/&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/DataTemplate.Triggers&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TreeViewItem.ItemTemplate&gt;
          &lt;/TreeViewItem&gt;
  </code></pre>
  
  <p>I am very new and very lost in this WPF thing.</p>