In my WPF application, I have a DataGrid (virtualization disabled for both rows and columns). The ItemsSource of the DataGrid is an ObservableCollection< ItemViewModel >. On my first column’s CellTemplate, I have some elements including this Button :
<Button Grid.Column="1"
Margin="0 0 4 0"
Visibility="{Binding CanView, Converter={StaticResource BoolToVisibilityConverter}}"
Style="{StaticResource ToolStripRowButtonStyle}"
Command="{Binding ViewCommand}"
CommandParameter="{Binding}">
<Image Source="pack://application:,,,/SomeApp;component/Resources/View.png"/>
</Button>
ViewCommand is a custom command imlementing ICommand. I implemented the CanExecuteChanged event such that a counter is incremented for each registration to the event and decremented for each unregistration from the event.
When ItemViewModels are added to the collection, I see that rows are created and I see my registration counter increment by one each time which is what I expect.
But when I remove some ItemViewModels from the collection, even though I see that the rows disappear from the UI, my registration counter never decrements. And indeed, if I trigger the CanExecuteChanged, I see that my Command.CanExecute(param) is called with the CommandParameter of older items that were previously removed. This confirms that the Framework did NOT unregister the old buttons from the commands’ CanExecuteChanged events.
Puzzling fact: I will sometimes see a decrement or two slide in, but never in a systematic way and never in a way that allowed me to understand what is going on.
I am genuinely expecting that as soon as an item is removed from the collection, its row would be destroyed and all its reference to its ItemViewModel would be released. I can’t understand why an old row is somewhat “staying alive”.
Extra info:
I am also providing this extra info that I am also observing and that I think should not happen, just in case it is somewhat related and helps someone figure out the issues.
In my second DataGrid column’s CellTemplate, I have this trigger:
<DataTrigger Binding="{Binding ElementName=m_dataGrid, Path=DataContext.CanEdit}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
Also when I remove an ItemViewModel from the collection, when the corresponding row is removed, I get an error about the binding not being resolved. I find it strange because since the row should be removed, I don’t understand why the Framework is still trying to resolve the binding. Once again, it is as if the row is somehow still alive.
The error I get is:
System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘ElementName=m_dataGrid’. BindingExpression:Path=DataContext.CanEdit; DataItem=null; target element is ‘DataGridCell’ (Name=”); target property is ‘NoTarget’ (type ‘Object’)