I’ve a tool bar which has its own view model. I’ve used command binding handle the clicks in the view model. I’ve used codeplex’s command class that comes up with the toolkit. When I close the window and open another window in the same application, I see my CanExecute() of the previously closed window’s method getting called. I’ve got it confirmed by using CommandManager.InvalidateRequerySuggested() that the CommandManager is still holding the reference!! CommandManager is supposed hold Weak reference but for some reason it is not releasing the delegates at all.
What have I done wrong here? How can I overcome this issue?
The reason why this is happening is that CommandManager has no clue that it should stop firing CanExecute until the handler gets garbage collected.
I had the same issue and I’ve solved it by setting DataContext of the window to
nulljust after closing the window. It works fine assuming that the commands are bound to ViewModel (removing he DataContext unregisters the canExecute event handlers).