title: Memory leak on CollectionView.View.Refresh
body: <p>I have defined my binding thus:</p>
  
  <pre><code>&lt;TreeView
          ItemsSource="{Binding UsersView.View}"
          ItemTemplate="{StaticResource MyDataTemplate}"
  /&gt;
  </code></pre>
  
  <p>The CollectionViewSource is defined thus:</p>
  
  <pre><code>private ObservableCollection&lt;UserData&gt; users;
  public CollectionViewSource UsersView{get;set;}
  UsersView=new CollectionViewSource{Source=users};
  UsersView.SortDescriptions.Add(
            new SortDescription("IsLoggedOn",ListSortDirection.Descending);
  UsersView.SortDescriptions.Add(
            new SortDescription("Username",ListSortDirection.Ascending);
  </code></pre>
  
  <p>So far, so good, this works as expected: The view shows first the users that are logged on in alphabetical order, then the ones that are not.<p></p>
  
  <p>However, the IsLoggedIn property of the UserData is updated every few seconds by a backgroundworker thread and then the code calls:</p>
  
  <pre><code>UsersView.View.Refresh();
  </code></pre>
  
  <p>on the UI thread.<p>
  Again this works as expected: users that log on are moved from the bottom of the view to the top and vice versa. However:  <em>Every time I call the Refresh method on the view the application hoards 3,5MB of extra memory, which is only released after application shutdown (or after an OutOfMemoryException...)</em><p></p>
  
  <p>I did some research and below is a list of fixes that did NOT work:</p>
  
  <ul>
  <li>The UserData class implements INotifyPropertyChanged</li>
  <li>Changing the underlying users collection does not make any difference at all: any IENumerable<code>&lt;UserData</code>> as a source for the CollectionViewSource causes the problem.<br/>
  -Changing the ColletionViewSource to a List<code>&lt;UserData</code>> (and refreshing the binding) or inheriting from ObservableCollection to get access to the underlying Items collection to sort that in place does not work.<P></li>
  </ul>
  
  <p>I am out of ideas! Help?</p>
  
  <p>EDIT:
  I found it:
  The Resource MyDataTemplate contains a Label that is bound to a UserData object to show one of its properties, the UserData objects being handed down by the TreeView's ItemsSource. The Label has a ContextMenu defined thus:</p>
  
  <pre><code> &lt;ContextMenu Background="Transparent" Width="325" Opacity=".8" HasDropShadow="True"&gt;
  
        &lt;PrivateMessengerUI:MyUserData IsReadOnly="True" &gt;
  
            &lt;PrivateMessengerUI:MyUserData.DataContext&gt;
  
                &lt;Binding Path="."/&gt;
  
            &lt;/PrivateMessengerUI:MyUserData.DataContext&gt;
  
       &lt;/PrivateMessengerUI:MyUserData&gt;
  
  &lt;/ContextMenu&gt;
  </code></pre>
  
  <p>The MyUserData object is a UserControl that shows All properties of the UserData object. In this way the user first only sees one piece of data of a user and on a right click sees all of it.</p>
  
  <p>When I remove the MyUserData UserControl from the DataTemplate the memory leak disappears! How can I still implement the behaviour as specified above? </p>