title: WPF DataGrid - highlight new rows when inserted into datagrid
body: <p>I have a datagrid bound to an ObservableCollection, and what I'd like to do is highlight new rows when they are added to the datagrid (i.e. when a new object is inserted into the ObservableCollection).  I'd like to highlight the rows when they are inserted by changing the background colour initially, but then having the colour fade back to normal over time.  I've tried a variety of different methods to get it to work but nothing quite works properly.</p>
  
  <p>Method 1: I have an event trigger that fires when the column loads.  It does fire when the element loads, but it seems to fire almost randomly on other old rows as well (rows on which it already fired once when the row was new).</p>
  
  <pre><code>&lt;DataGridHyperlinkColumn x:Name="OrderID" Binding="{Binding OrderNumber}" Header="Order" SortMemberPath="ciOrderId"&gt;
      &lt;DataGridHyperlinkColumn.ElementStyle&gt;                                
          &lt;Style TargetType="TextBlock"&gt;
              &lt;Setter Property="Background" Value="Transparent"/&gt;
              &lt;EventSetter Event="Hyperlink.Click" Handler="OrderNumber_Click" /&gt;
              &lt;Style.Triggers&gt;
                  &lt;EventTrigger RoutedEvent="Loaded"&gt;
                      &lt;BeginStoryboard&gt;
                          &lt;Storyboard&gt;
                              &lt;ColorAnimation 
                                              Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" 
                                              Duration="00:00:03" 
                                              From="Red" To="Transparent" /&gt;
                          &lt;/Storyboard&gt;                                                
                      &lt;/BeginStoryboard&gt;
                  &lt;/EventTrigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/DataGridHyperlinkColumn.ElementStyle&gt;
  &lt;/DataGridHyperlinkColumn&gt;
  </code></pre>
  
  <p>Method 2: I made a bool in the view model that is set to true when the new item is added to the ObservableCollection.  I then check this value in a trigger and if it is true I fire the storyboard.  I can't get this to work properly though, and the application keeps erroring out when I run it.  Also, I can't figure out a way to set this value to false once the storyboard has run (I can't use the storyboard's Completed event because the DataTrigger is in a style).</p>
  
  <pre><code>&lt;DataTrigger Binding="{Binding isNew}" Value="True"&gt;
      &lt;DataTrigger.EnterActions&gt;
          &lt;BeginStoryboard&gt;
              &lt;Storyboard&gt;
                  &lt;ColorAnimation
                      Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" 
                      Duration="00:00:03" 
                      From="Red" To="{x:Null}" FillBehavior="Stop"/&gt;
              &lt;/Storyboard&gt;
          &lt;/BeginStoryboard&gt;
      &lt;/DataTrigger.EnterActions&gt;
  &lt;/DataTrigger&gt;
  </code></pre>
  
  <p>Method 3: I tried setting a timestamp field in the view model when the new item is added to the observable collection.  Then in XAML I want to be able to compare that timestamp to the current time, and if it matches then I will fire the event.  I even have another field that contains the current time and is automatically updated by INotifyPropertyChanged, but I can't seem to figure out a way to compare the timestamp of the new row to the field containing the current time.</p>
  
  <p>I feel like there must be a solution to this, but after spending a frustrating day trying to figure it out I'm hoping someone out there will be able to shed some light.</p>