I have an observable collection of documents bound to DockSite.DocumentItemsSource but when the user closes a document i want to get rid of it completely. The user can close the document either by clicking a menu item that executes a command on the viewmodel or through the close button on the document window.
I tried to add a IsOpen property on my document viewmodel and bind that to IsOpen and then remove the document from the collection when notified with IsOpen == false, which I defer using Dispatcher.BeginInvoke, since I cannot change the collection that is bound on the raised property change event.
The problem is that the IsOpen dependency property is changed when the layout is updated (I.e. docking a window, etc.) and in addition to not really being what I want it also crashes the application in random ways, sometimes with null pointer exceptions and sometimes with WPF exceptions complaining that the logical tree is updated during a traversal (but not with any of my code in the callstacks so seems somehow the internal state gets corrupted). As an sidenote, I also tried to bind in a similar way with tool windows and in that case I never receive a change for IsOpen when the window is closed, so this is also not working just in a different way.
So I'm thinking that I'm not really suppose to bind to IsOpen in this way but then how am I suppose to manage my viewmodel? I can hook the events and I'm assuming (still haven't tested this) that at least these will not fire when the layout is changed because if they are I'm a bit unsure how I detect whether they are fired as a result of the user pressing a button or whether it is because of a layout change
But even if this is working, it is all really awkward in terms of MVVM, so I'm wondering if I'm missing something?