title: Keep same selected Item in a ListView when the collection it is bound to is refreshed
body: <p>I have a ListView that is bound to an ObservableCollection which is itself derived from an IQueryable. When I refresh the collection, after items are added, edited or a button is clicked (to see if the database has new items from other users). The listbox refreshes and selects the first item in the collection. </p>
  
  <p>I want to keep the same selected item (If it still exists) after a refresh but because the Collection has been replaced I cannot seem to compare equality between an item in the old collection and the new one. All attemtps never have a match.</p>
  
  <p>How should this be done?</p>
  
  <p>Below are some relevant code snipets if anyone wants to see them:</p>
  
  <p>The ListView, MouseDoubleClick Event opens an edit window</p>
  
  <pre><code>&lt;ListView x:Name="IssueListView"
            ItemsSource="{Binding Issues}"
            ItemTemplate="{StaticResource ShowIssueDetail}" 
            IsSynchronizedWithCurrentItem="True"
            MouseDoubleClick="IssueListView_MouseDoubleClick" /&gt;
  </code></pre>
  
  <p>It's DataTemplate</p>
  
  <pre><code>&lt;DataTemplate x:Key="ShowIssueDetail"&gt;
      &lt;Border CornerRadius="3" Margin="2" MinWidth="400" BorderThickness="2" 
          BorderBrush="{Binding Path=IssUrgency, 
                       Converter={StaticResource IntToRYGBBorderBrushConverter}}"&gt;
            &lt;StackPanel Orientation="Vertical"&gt;
               &lt;TextBlock Text="{Binding Path=IssSubject}" 
                          Margin="3" FontWeight="Bold" FontSize="14"/&gt;
  
            &lt;!--DataTrigger will collapse following panel for simple view--&gt;
            &lt;StackPanel Name="IssueDetailPanel" Visibility="Visible" Margin="3"&gt;
              &lt;StackPanel Width="Auto" Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="Due: " FontWeight="Bold"/&gt;
                  &lt;TextBlock Text="{Binding Path=IssDueDate, StringFormat='d'}"
                           FontStyle="Italic" HorizontalAlignment="Left"/&gt;
              &lt;/StackPanel&gt;
              &lt;StackPanel Width="Auto" Orientation="Horizontal"&gt;
                 &lt;TextBlock Text="Category: " FontWeight="Bold"/&gt;
                 &lt;TextBlock Text="{Binding Path=IssCategory}"/&gt;
              &lt;/StackPanel&gt;
            &lt;/StackPanel&gt;
  
          &lt;/StackPanel&gt;
          &lt;/Border&gt;
  
          &lt;DataTemplate.Triggers&gt;
              &lt;DataTrigger Binding="{Binding Path=StatusBoardViewModel.ShowDetailListItems, 
                           RelativeSource={RelativeSource FindAncestor,
                           AncestorType={x:Type Window}}}" Value="False"&gt;
                  &lt;Setter TargetName="IssueDetailPanel" 
                          Property="Visibility" Value="Collapsed"/&gt;
              &lt;/DataTrigger&gt;
          &lt;/DataTemplate.Triggers&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The Issues collection is refreshed by executing a QueryIssues Method that builds a Linq To SQL query programatically based on bound controls. IssuesQuery is an IQueryable property</p>
  
  <pre><code>public void QueryIssues()
  {
      // Will show all items
      IssuesQuery = from i in db.Issues
               orderby i.IssDueDate, i.IssUrgency
               select i;
  
      // Filters out closed issues if they are not to be shown
      if (includeClosedIssues == false)
      {
          IssuesQuery = from i in IssuesQuery
                   where i.IssIsClosed == false
                   select i;
      }
  
      // Filters out Regular Tasks if they are not to be shown
      if (showTasks == false)
      {
          IssuesQuery = from i in IssuesQuery
                   where i.IssIsOnStatusBoard == true
                   select i;
      }
  
      // Filters out private items if they are not to be shown
      if (showPrivateIssues == false)
      {
          IssuesQuery = from i in IssuesQuery
                   where i.IssIsPrivate == false
                   select i;
      }
  
      // Filters out Deaprtments if one is selected
      if (departmentToShow != "All")
      {
          IssuesQuery = from i in IssuesQuery
                   where i.IssDepartment == departmentToShow
                   select i;
      }
  
      Issues = new ObservableCollection&lt;Issue&gt;(IssuesQuery);
  }
  </code></pre>