Memory leak in CollectionView.View.Refresh

I defined my binding like this:

<TreeView
        ItemsSource="{Binding UsersView.View}"
        ItemTemplate="{StaticResource MyDataTemplate}"
/>

The CollectionViewSource element is defined as follows:

private ObservableCollection<UserData> 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);

So far, so good, it works as expected: first, users who logged in alphabetically and then those who are not are displayed in the view.

However, the IsLoggedIn UserData property is updated every few seconds by a background worker thread, and then calls the code:

UsersView.View.Refresh();

in the user interface thread.

, : , , . : , Refresh , 3,5 , ( OutOfMemoryException...)

, , :

  • UserData INotifyPropertyChanged
  • : IENumerable <UserData > CollectionViewSource .
    - ColletionViewSource List <UserData > ( ) ObservableCollection , .

! ?

EDIT: : Resource MyDataTemplate , UserData, , UserData TreeView ItemsSource. ContextMenu, :

 <ContextMenu Background="Transparent" Width="325" Opacity=".8" HasDropShadow="True">

      <PrivateMessengerUI:MyUserData IsReadOnly="True" >

          <PrivateMessengerUI:MyUserData.DataContext>

              <Binding Path="."/>

          </PrivateMessengerUI:MyUserData.DataContext>

     </PrivateMessengerUI:MyUserData>

</ContextMenu>

MyUserData UserControl, UserData. , , .

MyUserData UserControl DataTemplate, ! , ?

+3
2

- . . , , , ​​ , . , , ( ). , WPF, , ( ).

, .NET Memory Profiler ANTS Memory Profiler ( ). , , , . , . , , SO -.

+2

2 :

, DropShadow , , garbadge. . : http://blog.ramondeklein.nl/index.php/2009/02/20/memory-leak-with-wpf-resources-in-rare-cases/

HasDropShadow false , .

-, ContextMenu , ContextMenu StaticResource :

<ContextMenu Background="Transparent" Width="325" Opacity=".8" x:Key="MyAwesomeContextMenu">

      <PrivateMessengerUI:MyUserData IsReadOnly="True" >

          <PrivateMessengerUI:MyUserData.DataContext>

              <Binding Path="."/>

          </PrivateMessengerUI:MyUserData.DataContext>

     </PrivateMessengerUI:MyUserData>

</ContextMenu>

, :

ContextMenu="{StaticResource MyAwesomeContextMenu}"

, !

+1

Source: https://habr.com/ru/post/1736633/


All Articles