Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Memory leak on binding to a WPF TreeView

$
0
0

We're having an issue with a TreeView hanging onto a viewmodel set that has been bound to it. We have a static treeview on the main window, and reset its ItemsSource to Nothing or a new viewmodel when new things need to be displayed. The problem seems to be that it's hanging onto the first item that it was bound to, even after the ItemsSource is set to nothing. The connection seems to be through some strange unknown binding that's in the EffectiveValues collection of the TreeView.

 

I was able to repro this in a test project, so I'll post the simplified code:

 

TreeView templates:

 

<Window.Resources><HierarchicalDataTemplateDataType="{x:Type local:ParentViewModel}"ItemsSource="{Binding ChildVMs}"><TextBlockText="{Binding Name}"/></HierarchicalDataTemplate><DataTemplateDataType="{x:Type local:ChildViewModel}"><TextBlockText="{Binding Name}"/></DataTemplate></Window.Resources>

 

ViewModel definitions: (Shared Function to generate the itemssource for the treeview)

 

PublicClass ParentViewModelPublicShared counter AsInteger = 0PublicSharedFunction GenerateViewModel() As List(Of ParentViewModel)Dim pvm AsNew ParentViewModel With {.Name = "Parent"& counter} ' Unique name
  pvm.ChildVMs = New Generic.List(Of ChildViewModel)
  pvm.ChildVMs.Add(New ChildViewModel With {.Name = "Test 1", .Parent = pvm})
  pvm.ChildVMs.Add(New ChildViewModel With {.Name = "Test 2", .Parent = pvm})
  pvm.ChildVMs.Add(New ChildViewModel With {.Name = "Test 3", .Parent = pvm})Dim pvmList AsNew List(Of ParentViewModel)
  pvmList.Add(pvm)
  counter += 1Return pvmListEndFunctionPublicProperty ChildVMs As Generic.List(Of ChildViewModel)PublicProperty Name AsStringEndClassPublicClass ChildViewModelPublicProperty Parent As ParentViewModelPublicProperty Name AsStringEndClass

 

Code Behind for the .xaml.vb:

 

PrivateSub Load_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  myTreeView.ItemsSource = ParentViewModel.GenerateViewModel()EndSubPrivateSub UnLoad_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)  
  myTreeView.ItemsSource = Nothing
  GC.Collect() ' Put this in here just to see if it would help; It doesn'tEndSub

 

How I'm running the test: Start the app, click the Load button (sets the itemssource), expand/click around a bit on these nodes, click unload, click load again, click around, click unload, check the state of the app.

 

After doing this, it's always the first set of viewmodels that is still hanging around (shows the ParentViewModel, name = "Parent0"). It's still attached to the treeview in the following way: DirectCast(myTreeView.EffectiveValues(17).LocalValue,System.Windows.Data.BindingExpression).DataSource

 

It's not always #17, but you get the idea. The EffectiveValue.LocalValue is a binding expression, the datasource of which is usually the ParentViewModel instance. If I look at the Target of the binding expression, the TargetElement is the TreeView, and the TargetProperty is NoTarget (no idea what that is). Also, if I look at the BindingExpression's ParentBinding property (A Binding object), its Source is also the ParentViewModel.

 

Any ideas on how to clear this mystery binding from the effectivevalues? Thanks in advance.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>