I’ve got a flowchart-style application drawn on a WPF Canvas. (Basically boxes connected with lines). The boxes are hooked up to various events on the ‘model’. I need to able to clear the Canvas, without destroying the model.
What I do is remove all the boxes etc from the Canvas’ Children collection. This works fine visually, but the controls seems to live on in limbo somewhere in memory. When I later fire events on the model, the controls crash my application. (the now-invisible boxes ‘expect’ to be the Child of a Canvas).
How should I indicate the controls are no longer wanted? I want them destroyed, not responding to events and bindings. Are the bindings to the model preventing WPF disposing of them? Would setting their Datacontext to null help?
Cheers, Jeff.
I’ve got a flowchart-style application drawn on a WPF Canvas. (Basically boxes connected with
Share
Sounds like you may need to have the boxes implement IDisposable and call Dispose on the boxes as you are removing them. In the dispose method you should unhook your event listeners. Or if you intend to boxes to be used again you may need to just expose some method to RemoveListeners() and then a method to AttachListeners() when you want it back in view.